> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI

> Trace OpenAI Python, Node.js, and Go SDK calls (including Azure OpenAI) with OpenInference and send spans to Arize AX for LLM observability.

[OpenAI](https://openai.com/) provides the GPT family of large language models through the [OpenAI Python SDK](https://github.com/openai/openai-python), [OpenAI Node.js SDK](https://github.com/openai/openai-node), and the [official OpenAI Go SDK](https://github.com/openai/openai-go). Arize AX captures every OpenAI SDK call — Responses API and chat completions calls, tool calls, Decisions API calls, and token usage — via the OpenInference instrumentors for [Python](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai), [JavaScript / TypeScript](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-instrumentation-openai), and [Go](https://github.com/Arize-ai/openinference/tree/main/go/openinference-instrumentation-openai-go). The Python, TypeScript, and Go instrumentors all cover Azure OpenAI.

> [card] [OpenAI Python Tracing Tutorial (Google Colab)](http://colab.research.google.com/github/Arize-ai/tutorials/blob/main/python/llm/tracing/openai/openai-tracing.ipynb)

> [card] [OpenAI Python Tracing Tutorials on GitHub](https://github.com/Arize-ai/tutorials/tree/main/python/llm/tracing/openai)

## Prerequisites

* Python 3.9+, Node.js 18+, or Go 1.25+
* An Arize AX account ([sign up](https://arize.com/sign-up/))
* An `OPENAI_API_KEY` from the [OpenAI Platform](https://platform.openai.com/api-keys), or Azure OpenAI credentials

## Launch Arize AX

1. Sign in to your [Arize AX account](https://app.arize.com/).
2. From **Space Settings**, copy your **Space ID** and **API Key**. You will set them as `ARIZE_SPACE_ID` and `ARIZE_API_KEY` below.

## Install

```bash Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
pip install arize-otel openinference-instrumentation-openai openai
```

```bash TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
npm install openai \
  @arizeai/openinference-instrumentation-openai \
  @arizeai/openinference-semantic-conventions \
  @opentelemetry/api \
  @opentelemetry/context-async-hooks \
  @opentelemetry/exporter-trace-otlp-proto \
  @opentelemetry/instrumentation \
  @opentelemetry/resources \
  @opentelemetry/sdk-trace \
  @opentelemetry/semantic-conventions
```

```bash Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
go get \
  github.com/Arize-ai/arize-otel-go \
  github.com/Arize-ai/openinference/go/openinference-instrumentation-openai-go \
  github.com/openai/openai-go/v3
```

## Configure credentials

> [tab] OpenAI
>
> ```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> export ARIZE_SPACE_ID="<your-space-id>"
> export ARIZE_API_KEY="<your-api-key>"
> export ARIZE_PROJECT_NAME="openai-tracing-example"
> export OPENAI_API_KEY="<your-openai-api-key>"
> ```

> [tab] Azure OpenAI
>
> ```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> export ARIZE_SPACE_ID="<your-space-id>"
> export ARIZE_API_KEY="<your-api-key>"
> export ARIZE_PROJECT_NAME="openai-tracing-example"
> export AZURE_OPENAI_API_KEY="<your-azure-key>"
> export AZURE_OPENAI_ENDPOINT="<your-azure-endpoint>"
> export OPENAI_API_VERSION="<api-version>"
> ```
>
> In the example below, swap the standard OpenAI client for the Azure client — `openai.AzureOpenAI()` in Python, `new AzureOpenAI(...)` in TypeScript, or `openai.NewClient(azure.WithEndpoint(...), azure.WithAPIKey(...), option.WithMiddleware(...))` using the [`openai-go/v3/azure`](https://pkg.go.dev/github.com/openai/openai-go/v3/azure) helpers in Go. The same instrumentor covers all three. The Go middleware recognizes Azure host suffixes (`*.openai.azure.com`, `*.services.ai.azure.com`, `*.cognitiveservices.azure.com`) and sets `llm.provider=azure` on those spans so backend queries can distinguish them from direct OpenAI traffic. The Python and TypeScript Azure clients read the API version from `OPENAI_API_VERSION` automatically; the Go Azure helper takes it as the second argument to `azure.WithEndpoint(endpoint, apiVersion)`.

## Setup tracing

```python Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
# instrumentation.py
import os

from arize.otel import register
from openinference.instrumentation.openai import OpenAIInstrumentor

tracer_provider = register(
    space_id=os.environ["ARIZE_SPACE_ID"],
    api_key=os.environ["ARIZE_API_KEY"],
    project_name=os.environ["ARIZE_PROJECT_NAME"],
)

OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
print("Arize AX tracing initialized for OpenAI.")
```

```typescript TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// instrumentation.ts
import { context, trace } from "@opentelemetry/api";
import {
  AsyncLocalStorageContextManager,
} from "@opentelemetry/context-async-hooks";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { resourceFromAttributes } from "@opentelemetry/resources";
import { SimpleSpanProcessor, TracerProvider } from "@opentelemetry/sdk-trace";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import {
  SEMRESATTRS_PROJECT_NAME,
} from "@arizeai/openinference-semantic-conventions";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import {
  OpenAIInstrumentation,
} from "@arizeai/openinference-instrumentation-openai";
import OpenAI from "openai";

const projectName =
  process.env.ARIZE_PROJECT_NAME ?? "openai-tracing-example";

export const provider = new TracerProvider({
  resource: resourceFromAttributes({
    [ATTR_SERVICE_NAME]: projectName,
    [SEMRESATTRS_PROJECT_NAME]: projectName,
  }),
  spanProcessors: [
    new SimpleSpanProcessor({
      exporter: new OTLPTraceExporter({
        url: "https://otlp.arize.com/v1/traces",
        headers: {
          "arize-space-id": process.env.ARIZE_SPACE_ID ?? "",
          "arize-api-key": process.env.ARIZE_API_KEY ?? "",
        },
      }),
    }),
  ],
});

trace.setGlobalTracerProvider(provider);
context.setGlobalContextManager(
  new AsyncLocalStorageContextManager().enable(),
);

const instrumentation = new OpenAIInstrumentation();
instrumentation.manuallyInstrument(OpenAI);

registerInstrumentations({ instrumentations: [instrumentation] });

console.log("Arize AX tracing initialized for OpenAI.");
```

```go Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// main.go
//
// Go integrations live in a single file — the tracer setup, the
// instrumented middleware, and the model call all sit inside main().
// The `Run OpenAI` step below is just `go run main.go`.
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    arizeotel "github.com/Arize-ai/arize-otel-go"
    openaiotel "github.com/Arize-ai/openinference/go/openinference-instrumentation-openai-go"
    "github.com/openai/openai-go/v3"
    "github.com/openai/openai-go/v3/option"
    "github.com/openai/openai-go/v3/responses"
    "go.opentelemetry.io/otel"
)

func main() {
    ctx := context.Background()

    projectName := os.Getenv("ARIZE_PROJECT_NAME")
    if projectName == "" {
        projectName = "openai-tracing-example"
    }

    // Register reads ARIZE_SPACE_ID / ARIZE_API_KEY from the environment.
    tp, err := arizeotel.Register(ctx, arizeotel.Options{
        ProjectName: projectName,
    })
    if err != nil {
        log.Printf("register tracer: %v", err)
        return
    }
    defer func() {
        shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
        defer cancel()
        _ = tp.Shutdown(shutdownCtx)
    }()

    fmt.Println("Arize AX tracing initialized for OpenAI.")

    // The middleware wraps every /v1/responses request in an
    // LLM-kind span. Reads OPENAI_API_KEY from the environment.
    client := openai.NewClient(
        option.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
        option.WithMiddleware(openaiotel.Middleware(otel.Tracer(projectName))),
    )

    resp, err := client.Responses.New(ctx, responses.ResponseNewParams{
        Model: "gpt-6.1-sol",
        Input: responses.ResponseNewParamsInputUnion{
            OfString: openai.String("Write a haiku about observability."),
        },
    })
    if err != nil {
        log.Printf("openai: %v", err)
        return
    }

    fmt.Println(resp.OutputText())
}
```

> [!NOTE]
>
> **Go SDK** Only `/v1/chat/completions`, `/v1/responses`, and `/v1/decisions` are instrumented; embeddings and image endpoints fall through unwrapped. Streaming Chat Completions responses pass through unchanged, but `output.value` and token counts are not populated for those spans yet; streamed Responses spans record their output and token counts.

## Run OpenAI

```python Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
# example.py

# Importing instrumentation first ensures tracing is set up
# before `openai` is imported.
from instrumentation import tracer_provider

import openai

# The client reads OPENAI_API_KEY from the environment.
client = openai.OpenAI()

response = client.responses.create(
    model="gpt-6.1-sol",
    input="Write a haiku about observability.",
)

print(response.output_text)
```

```typescript TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// example.ts

// Importing instrumentation first ensures tracing is set up
// before the OpenAI client is used.
import { provider } from "./instrumentation";
import OpenAI from "openai";

// The client reads OPENAI_API_KEY from the environment.
const client = new OpenAI();

const response = await client.responses.create({
  model: "gpt-6.1-sol",
  input: "Write a haiku about observability.",
});

console.log(response.output_text);

// Flush any pending spans before the process exits.
await provider.forceFlush();
```

```bash Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
# The full example lives in main.go above.
go run main.go
```

### Expected output

```text wrap theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
Arize AX tracing initialized for OpenAI.
Logs whisper softly,
metrics rise like morning mist —
truth in every span.
```

## Verify in Arize AX

1. Open your Arize AX space and select project **`openai-tracing-example`**.
2. You should see a new trace within \~30 seconds containing an LLM span — `Response` for the Python SDK, `OpenAI Responses` for the Node.js SDK, or `openai.responses.create` for the Go SDK — with the prompt, response, and token usage attached.
3. If no traces appear, see [Troubleshooting](#troubleshooting).

![OpenAI tracing in Arize AX](https://storage.googleapis.com/arize-phoenix-assets/assets/images/arize-docs-images/openai-tracing.gif)

### Check from the skill, CLI, or SDK

Confirm spans are actually reaching your Arize AX project. Use whichever fits your workflow — the skill and CLI work for any framework; the SDK check is shown for each language.

> [tab] Arize skill (agent)
>
> Install the [Arize Skills](https://github.com/Arize-ai/arize-skills) plugin and let your coding agent check for you:
>
> ```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> npx skills add Arize-ai/arize-skills
> ```
>
> Then prompt your agent:
>
> > Use the `arize-trace` skill to export and analyze recent traces from my project. Confirm spans are arriving, and summarize any errors or latency issues.

> [tab] AX CLI
>
> Export recent spans for your project — any rows mean traces are landing:
>
> ```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> ax spans export "$ARIZE_PROJECT_NAME" --space "$ARIZE_SPACE_ID" \
>   --limit 5 --stdout | jq 'length'
> ```
>
> A non-zero count confirms spans reached Arize AX. Run `ax auth login` first if you have not authenticated. See the [`ax spans` reference](/docs/api-clients/cli/spans).

> [tab] SDK
>
> Query the project's spans and check that at least one came back.
>
> ```python Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> import os
> from arize import ArizeClient
>
> client = ArizeClient(api_key=os.environ["ARIZE_API_KEY"])
> resp = client.spans.list(
>     project=os.environ["ARIZE_PROJECT_NAME"],
>     space=os.environ["ARIZE_SPACE_ID"],
>     limit=5,
> )
> count = len(resp.spans)
> print(
>     f"{count} span(s) found" if count else "No spans yet — recheck setup"
> )
> ```
>
> ```typescript TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> // Reads ARIZE_API_KEY from the environment.
> import { listSpans } from "@arizeai/ax-client";
>
> const { data: spans } = await listSpans({
>   project: process.env.ARIZE_PROJECT_NAME!,
>   space: process.env.ARIZE_SPACE_ID!,
>   limit: 5,
> });
> const count = spans.length;
> console.log(
>   count ? `${count} span(s) found` : "No spans yet — recheck setup",
> );
> ```
>
> ```go Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> client, err := arize.NewClient(
>     arize.Config{APIKey: os.Getenv("ARIZE_API_KEY")},
> )
> if err != nil {
>     log.Fatal(err)
> }
> resp, err := client.Spans.List(ctx, spans.ListRequest{
>     Project: os.Getenv("ARIZE_PROJECT_NAME"),
>     Space:   os.Getenv("ARIZE_SPACE_ID"),
>     Limit:   5,
> })
> if err != nil {
>     log.Fatal(err)
> }
> fmt.Printf("%d span(s) found\n", len(resp.Spans))
> ```
>
> SDK span references: [Python](/docs/api-clients/python/version-8/client-resources/spans.md) · [TypeScript](/docs/api-clients/typescript/version-1/client-resources/spans.md) · [Go](/docs/api-clients/go/version-2/client-resources/spans.md).

## Trace tool usage

The instrumentor traces each `responses.create` call automatically, including the tool calls the model requests. It does not trace your application *executing* those tools, or the loop that feeds results back to the model. To capture the full agent trace, wrap the loop in a manual chain span and each tool execution in a manual tool span with the [OpenTelemetry API](/docs/ax/instrument/manual-instrumentation.md) — the auto LLM spans nest under your chain span automatically. See [Combine auto and manual instrumentation](/docs/ax/instrument/combining-auto-and-manual.md) for the pattern.

```python Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
# tools_example.py

# Importing instrumentation first ensures tracing is set up
# before `openai` is imported.
from instrumentation import tracer_provider

import json

import openai
from opentelemetry import trace
from openinference.instrumentation import OITracer, TraceConfig

# The client reads OPENAI_API_KEY from the environment.
client = openai.OpenAI()
tracer = OITracer(trace.get_tracer(__name__), config=TraceConfig())


# A trivial tool the model can call.
@tracer.tool
def get_weather(city: str) -> str:
    return f"It is 72°F and sunny in {city}."


tools = [
    {
        "type": "function",
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
        "strict": False,
    },
]


@tracer.chain
def weather_agent(question: str) -> str:
    # First LLM call — auto-instrumented as an LLM span.
    response = client.responses.create(
        model="gpt-6.1-sol",
        input=question,
        tools=tools,
    )

    # Execute each tool call inside a manual TOOL span.
    tool_outputs = []
    for item in response.output:
        if item.type != "function_call":
            continue
        args = json.loads(item.arguments)
        result = get_weather(**args)
        tool_outputs.append({
            "type": "function_call_output",
            "call_id": item.call_id,
            "output": result,
        })

    # Second LLM call — the model answers using the tool result.
    # previous_response_id carries the conversation forward.
    final = client.responses.create(
        model="gpt-6.1-sol",
        previous_response_id=response.id,
        input=tool_outputs,
        tools=tools,
    )
    return final.output_text


print(weather_agent("What is the weather in San Francisco?"))
```

```typescript TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// tools_example.ts

// Importing instrumentation first ensures tracing is set up
// before the OpenAI client is used.
import { provider } from "./instrumentation";
import OpenAI from "openai";
import { SpanStatusCode, trace } from "@opentelemetry/api";
import {
  OpenInferenceSpanKind,
  SemanticConventions,
} from "@arizeai/openinference-semantic-conventions";

// The client reads OPENAI_API_KEY from the environment.
const client = new OpenAI();
const tracer = trace.getTracer("weather-agent");

// A trivial tool the model can call.
async function getWeather(city: string): Promise<string> {
  await new Promise((resolve) => setTimeout(resolve, 100)); // Simulate a network call
  return `It is 72°F and sunny in ${city}.`;
}

const tools: OpenAI.Responses.Tool[] = [
  {
    type: "function",
    name: "get_weather",
    description: "Get the current weather for a city.",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
    strict: false,
  },
];

async function runAgent(question: string): Promise<string> {
  // Manual CHAIN span wraps the whole tool loop.
  return await tracer.startActiveSpan("weather-agent", async (chainSpan) => {
    chainSpan.setAttribute(
      SemanticConventions.OPENINFERENCE_SPAN_KIND,
      OpenInferenceSpanKind.CHAIN,
    );
    chainSpan.setAttribute(SemanticConventions.INPUT_VALUE, question);

    // First LLM call — auto-instrumented as an LLM span.
    const response = await client.responses.create({
      model: "gpt-6.1-sol",
      input: question,
      tools,
    });

    // Execute each tool call inside a manual TOOL span.
    const toolOutputs: OpenAI.Responses.ResponseInputItem[] = [];
    for (const item of response.output) {
      if (item.type !== "function_call") continue;
      const args = JSON.parse(item.arguments);
      const result = await tracer.startActiveSpan(
        item.name,
        async (toolSpan) => {
          toolSpan.setAttribute(
            SemanticConventions.OPENINFERENCE_SPAN_KIND,
            OpenInferenceSpanKind.TOOL,
          );
          toolSpan.setAttribute(
            SemanticConventions.INPUT_VALUE,
            item.arguments,
          );
          const r = await getWeather(args.city);
          toolSpan.setAttribute(SemanticConventions.OUTPUT_VALUE, r);
          toolSpan.setStatus({ code: SpanStatusCode.OK });
          toolSpan.end();
          return r;
        },
      );
      toolOutputs.push({
        type: "function_call_output",
        call_id: item.call_id,
        output: result,
      });
    }

    // Second LLM call — the model answers using the tool result.
    // previous_response_id carries the conversation forward.
    const final = await client.responses.create({
      model: "gpt-6.1-sol",
      previous_response_id: response.id,
      input: toolOutputs,
      tools,
    });
    const answer = final.output_text;
    chainSpan.setAttribute(SemanticConventions.OUTPUT_VALUE, answer);
    chainSpan.setStatus({ code: SpanStatusCode.OK });
    chainSpan.end();

    // Flush any pending spans before the process exits.
    await provider.forceFlush();
    return answer;
  });
}

console.log(await runAgent("What is the weather in San Francisco?"));
```

```go Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// main.go
//
// Self-contained tool loop: tracer setup, a manual CHAIN span, and a manual
// TOOL span around the tool execution — all in one file. Run with
// `go run main.go`.
package main

import (
    "context"
    "encoding/json"
    "fmt"
    "log"
    "os"
    "time"

    arizeotel "github.com/Arize-ai/arize-otel-go"
    openaiotel "github.com/Arize-ai/openinference/go/openinference-instrumentation-openai-go"
    semconv "github.com/Arize-ai/openinference/go/openinference-semantic-conventions"
    "github.com/openai/openai-go/v3"
    "github.com/openai/openai-go/v3/option"
    "github.com/openai/openai-go/v3/responses"
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/attribute"
    "go.opentelemetry.io/otel/codes"
)

// A trivial tool the model can call.
func getWeather(city string) string {
    return fmt.Sprintf("It is 72°F and sunny in %s.", city)
}

func main() {
    ctx := context.Background()

    projectName := os.Getenv("ARIZE_PROJECT_NAME")
    if projectName == "" {
        projectName = "openai-tracing-example"
    }

    tp, err := arizeotel.Register(ctx, arizeotel.Options{ProjectName: projectName})
    if err != nil {
        log.Printf("register tracer: %v", err)
        return
    }
    defer func() {
        shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
        defer cancel()
        _ = tp.Shutdown(shutdownCtx)
    }()

    fmt.Println("Arize AX tracing initialized for OpenAI.")

    tracer := otel.Tracer(projectName)
    client := openai.NewClient(
        option.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
        option.WithMiddleware(openaiotel.Middleware(tracer)),
    )

    tools := []responses.ToolUnionParam{
        responses.ToolParamOfFunction("get_weather", map[string]any{
            "type": "object",
            "properties": map[string]any{
                "city": map[string]any{"type": "string"},
            },
            "required": []string{"city"},
        }, false),
    }

    question := "What is the weather in San Francisco?"

    // Manual CHAIN span wraps the whole tool loop. Threading this ctx into the
    // client calls nests the auto LLM spans under it.
    ctx, chainSpan := tracer.Start(ctx, "weather-agent")
    chainSpan.SetAttributes(
        attribute.String(semconv.OpenInferenceSpanKind, semconv.SpanKindChain),
        attribute.String(semconv.InputValue, question),
    )

    // First LLM call — auto-instrumented as an LLM span.
    resp, err := client.Responses.New(ctx, responses.ResponseNewParams{
        Model: "gpt-6.1-sol",
        Input: responses.ResponseNewParamsInputUnion{OfString: openai.String(question)},
        Tools: tools,
    })
    if err != nil {
        log.Printf("openai: %v", err)
        return
    }

    // Execute each tool call inside a manual TOOL span.
    var toolOutputs responses.ResponseInputParam
    for _, item := range resp.Output {
        if item.Type != "function_call" {
            continue
        }
        call := item.AsFunctionCall()
        var input struct {
            City string `json:"city"`
        }
        _ = json.Unmarshal([]byte(call.Arguments), &input)

        _, toolSpan := tracer.Start(ctx, call.Name)
        toolSpan.SetAttributes(
            attribute.String(semconv.OpenInferenceSpanKind, semconv.SpanKindTool),
            attribute.String(semconv.InputValue, call.Arguments),
        )
        result := getWeather(input.City)
        toolSpan.SetAttributes(attribute.String(semconv.OutputValue, result))
        toolSpan.SetStatus(codes.Ok, "")
        toolSpan.End()

        output := responses.ResponseInputItemParamOfFunctionCallOutput(result)
        output.OfFunctionCallOutput.CallID = openai.String(call.CallID)
        toolOutputs = append(toolOutputs, output)
    }

    // Second LLM call — the model answers using the tool result.
    // PreviousResponseID carries the conversation forward.
    final, err := client.Responses.New(ctx, responses.ResponseNewParams{
        Model:              "gpt-6.1-sol",
        PreviousResponseID: openai.String(resp.ID),
        Input:              responses.ResponseNewParamsInputUnion{OfInputItemList: toolOutputs},
        Tools:              tools,
    })
    if err != nil {
        log.Printf("openai: %v", err)
        return
    }

    answer := final.OutputText()
    chainSpan.SetAttributes(attribute.String(semconv.OutputValue, answer))
    chainSpan.SetStatus(codes.Ok, "")
    chainSpan.End()

    fmt.Println(answer)
}
```

### Expected output

```text wrap theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
Arize AX tracing initialized for OpenAI.
The weather in San Francisco is currently 72°F and sunny.
```

The trace tree in Arize AX is a **`weather-agent`** chain span (**`weather_agent`** in Python, named after the decorated function) holding two LLM spans and, between them, one **`get_weather`** tool span.

> [!NOTE]
>
> **Go SDK** The chain and tool spans are created manually with the OpenTelemetry API — threading the chain span's `ctx` into each request is what nests the auto LLM spans underneath it.

## Trace decisions

The [OpenAI Decisions API](https://developers.openai.com/api/docs/guides/decisions) answers a fixed set of typed questions about an input instead of generating text. Each question is a yes/no `predicate`, a `choice` among labeled options, or a `score` against labeled levels, and each answer comes back with its probabilities. The instrumentor traces each `decisions.create` call as a [decision span](/docs/ax/concepts/otel-openinference/span-kinds.md#decision) instead of an LLM span. You don't need any extra setup: in Python and TypeScript the `instrumentation` file above covers it, and the Go example is self-contained like the other Go examples.

Decision tracing needs these versions:

* **Python:** `openinference-instrumentation-openai` 0.1.64 or later, with `openai` 3.26.0 or later.
* **TypeScript:** `@arizeai/openinference-instrumentation-openai` 4.4.0 or later, with `openai` 7.30.0 or later. `openai` 7 requires Node.js 22 or later.
* **Go:** `openinference-instrumentation-openai-go` v0.1.1 or later, with `github.com/openai/openai-go/v3` 3.73.0 or later.

```python Python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
# decisions_example.py

# Importing instrumentation first ensures tracing is set up
# before `openai` is imported.
from instrumentation import tracer_provider

import openai

# The client reads OPENAI_API_KEY from the environment.
client = openai.OpenAI()

decision = client.decisions.create(
    model="gpt-6-luna",
    input="I was charged twice for my order.",
    questions=[
        {
            "type": "predicate",
            "name": "is_refund",
            "instructions": "Is this a refund request?",
        },
        {
            "type": "choice",
            "name": "department",
            "instructions": "Which department should handle this complaint?",
            "choices": [
                {"value": "billing", "description": "Payments, invoices, and refunds."},
                {"value": "technical", "description": "Problems using the product."},
                {"value": "shipping", "description": "Delivery and tracking."},
            ],
        },
        {
            "type": "score",
            "name": "urgency",
            "instructions": "How urgent is this message?",
            "levels": [
                {"label": "low", "description": "Can wait a week."},
                {"label": "medium"},
                {"label": "high", "description": "Needs a reply today."},
            ],
        },
    ],
)

for answer in decision.answers:
    print(answer.model_dump_json())
```

```typescript TypeScript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// decisions_example.ts

// Importing instrumentation first ensures tracing is set up
// before the OpenAI client is used.
import { provider } from "./instrumentation";
import OpenAI from "openai";

// The client reads OPENAI_API_KEY from the environment.
const client = new OpenAI();

const decision = await client.decisions.create({
  model: "gpt-6-luna",
  input: "I was charged twice for my order.",
  questions: [
    {
      type: "predicate",
      name: "is_refund",
      instructions: "Is this a refund request?",
    },
    {
      type: "choice",
      name: "department",
      instructions: "Which department should handle this complaint?",
      choices: [
        { value: "billing", description: "Payments, invoices, and refunds." },
        { value: "technical", description: "Problems using the product." },
        { value: "shipping", description: "Delivery and tracking." },
      ],
    },
    {
      type: "score",
      name: "urgency",
      instructions: "How urgent is this message?",
      levels: [
        { label: "low", description: "Can wait a week." },
        { label: "medium" },
        { label: "high", description: "Needs a reply today." },
      ],
    },
  ],
});

for (const answer of decision.answers) {
  console.log(JSON.stringify(answer));
}

// Flush any pending spans before the process exits.
await provider.forceFlush();
```

```go Go theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
// decisions/main.go
//
// Self-contained Decisions example: tracer setup, the instrumented
// client, and one decisions call, all in one file. Run with
// `go run ./decisions`.
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    arizeotel "github.com/Arize-ai/arize-otel-go"
    openaiotel "github.com/Arize-ai/openinference/go/openinference-instrumentation-openai-go"
    "github.com/openai/openai-go/v3"
    "github.com/openai/openai-go/v3/option"
    "go.opentelemetry.io/otel"
)

func main() {
    ctx := context.Background()

    projectName := os.Getenv("ARIZE_PROJECT_NAME")
    if projectName == "" {
        projectName = "openai-tracing-example"
    }

    tp, err := arizeotel.Register(ctx, arizeotel.Options{ProjectName: projectName})
    if err != nil {
        log.Printf("register tracer: %v", err)
        return
    }
    defer func() {
        shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
        defer cancel()
        _ = tp.Shutdown(shutdownCtx)
    }()

    fmt.Println("Arize AX tracing initialized for OpenAI.")

    // The middleware wraps every /v1/decisions request in a DECISION-kind
    // span. Reads OPENAI_API_KEY from the environment.
    client := openai.NewClient(
        option.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
        option.WithMiddleware(openaiotel.Middleware(otel.Tracer(projectName))),
    )

    decision, err := client.Decisions.New(ctx, openai.DecisionNewParams{
        Model: "gpt-6-luna",
        Input: openai.DecisionNewParamsInputUnion{
            OfString: openai.String("I was charged twice for my order."),
        },
        Questions: []openai.DecisionNewParamsQuestionUnion{
            {OfPredicate: &openai.DecisionNewParamsQuestionPredicate{
                Name: openai.String("is_refund"),
                Instructions: openai.DecisionNewParamsQuestionPredicateInstructionsUnion{
                    OfString: openai.String("Is this a refund request?"),
                },
            }},
            {OfChoice: &openai.DecisionNewParamsQuestionChoice{
                Name: openai.String("department"),
                Instructions: openai.DecisionNewParamsQuestionChoiceInstructionsUnion{
                    OfString: openai.String("Which department should handle this complaint?"),
                },
                Choices: []openai.DecisionNewParamsQuestionChoiceChoice{
                    {Value: openai.DecisionNewParamsQuestionChoiceChoiceValueUnion{OfString: openai.String("billing")}, Description: openai.String("Payments, invoices, and refunds.")},
                    {Value: openai.DecisionNewParamsQuestionChoiceChoiceValueUnion{OfString: openai.String("technical")}, Description: openai.String("Problems using the product.")},
                    {Value: openai.DecisionNewParamsQuestionChoiceChoiceValueUnion{OfString: openai.String("shipping")}, Description: openai.String("Delivery and tracking.")},
                },
            }},
            {OfScore: &openai.DecisionNewParamsQuestionScore{
                Name: openai.String("urgency"),
                Instructions: openai.DecisionNewParamsQuestionScoreInstructionsUnion{
                    OfString: openai.String("How urgent is this message?"),
                },
                Levels: []openai.DecisionNewParamsQuestionScoreLevel{
                    {Label: "low", Description: openai.String("Can wait a week.")},
                    {Label: "medium"},
                    {Label: "high", Description: openai.String("Needs a reply today.")},
                },
            }},
        },
    })
    if err != nil {
        log.Printf("openai: %v", err)
        return
    }

    for _, answer := range decision.Answers {
        fmt.Println(answer.RawJSON())
    }
}
```

### Expected output

The example prints one answer per question, each as a single line of JSON. The Python output is below, formatted for readability; the TypeScript and Go examples print the same answers with the fields in a different order. Your probabilities may differ:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
Arize AX tracing initialized for OpenAI.
{
  "name": "is_refund",
  "probability": 0.46,
  "type": "predicate"
}
{
  "choice": "billing",
  "confidence": 1.0,
  "name": "department",
  "probabilities": [
    { "probability": 1.0, "value": "billing" },
    { "probability": 0.0, "value": "technical" },
    { "probability": 0.0, "value": "shipping" }
  ],
  "type": "choice"
}
{
  "confidence": 0.52,
  "name": "urgency",
  "probabilities": [
    { "label": "low", "probability": 0.0, "value": 0 },
    { "label": "medium", "probability": 0.32, "value": 1 },
    { "label": "high", "probability": 0.68, "value": 2 }
  ],
  "score": 1.68,
  "type": "score"
}
```

In Arize AX, the trace holds a single span, `Decision` for the Python SDK, `OpenAI Decisions` for the Node.js SDK, or `openai.decisions.create` for the Go SDK, with `openinference.span.kind` set to `DECISION`. It carries:

* `input.value`: the request (`model`, `input`, and `questions`) as JSON
* `output.value`: the response (the `answers` and their probabilities) as JSON
* `decision.system` and `decision.provider`: both `openai`
* `decision.request.model_name`, `decision.response.model_name`, and `decision.model_name`: the model, for example `gpt-6-luna`
* `decision.token_count.input` and `decision.token_count.output`: the token usage. There is no total; add the two if you need one.

The span has no `llm.*` model or token attributes, so decision calls aren't counted as LLM calls. See [DECISION](/docs/ax/concepts/otel-openinference/span-kinds.md#decision) for the full attribute list.

## Troubleshooting

* **No traces in Arize AX.** Confirm `ARIZE_SPACE_ID` and `ARIZE_API_KEY` are set in the same shell that runs the example. Enable OpenTelemetry debug logs with `export OTEL_LOG_LEVEL=debug` and re-run.
* **OpenAI spans missing but other spans present (Python).** `OpenAIInstrumentor().instrument(...)` must run before any `import openai` in the application. Make sure `instrumentation.py` is the first import in your entry point.
* **OpenAI spans missing but other spans present (TypeScript).** `instrumentation.manuallyInstrument(OpenAI)` must run before any code creates an `OpenAI` client. Make sure `import { provider } from "./instrumentation"` (or a side-effect-only `import "./instrumentation"`) is the first import in your entry point.
* **`401` from OpenAI.** Verify `OPENAI_API_KEY` is set and has access to the model in the example. Swap `gpt-6.1-sol` for a model your key can call.
* **Azure OpenAI returns `Resource not found`.** Confirm `AZURE_OPENAI_ENDPOINT` points to your deployment, `OPENAI_API_VERSION` matches a version your deployment supports, and the example uses the Azure client constructor (`openai.AzureOpenAI()` / `new AzureOpenAI()`) rather than the standard OpenAI client.
* **TypeScript process exits before spans flush.** With `SimpleSpanProcessor`, spans are sent immediately, but make sure to `await provider.forceFlush()` (or call `provider.shutdown()`) before the process exits to avoid losing trailing spans.
* **Go process exits before spans flush.** `arize-otel-go` uses a batched span processor by default. The `defer tp.Shutdown(...)` block in `main.go` is what flushes the batch — without it, short-lived programs lose their last spans. Pass `SimpleProcessor: true` to `arizeotel.Register` if you want synchronous export instead.
* **Go example wraps the wrong endpoint.** The Go middleware instruments `/v1/chat/completions`, `/v1/responses`, and `/v1/decisions`. Embeddings, completions, and image endpoints pass through to the next middleware unchanged, and no span is emitted for them.

## Resources

> [card] [OpenAI Python SDK](https://github.com/openai/openai-python)

> [card] [OpenAI Node.js SDK](https://github.com/openai/openai-node)

> [card] [OpenInference OpenAI Instrumentor (Python)](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai)

> [card] [OpenInference OpenAI Instrumentor (JS/TS)](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-instrumentation-openai)

> [card] [OpenAI Go SDK](https://github.com/openai/openai-go)

> [card] [OpenInference OpenAI Instrumentor (Go)](https://github.com/Arize-ai/openinference/tree/main/go/openinference-instrumentation-openai-go)
