openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

šŸ› Bug Report:OpenAI instrumentation does not record gen_ai.request.seed, gen_ai.request.stop_sequences or gen_ai.output.type

Open armanunix opened this issue 2 months ago • 2 comments

Which component is this bug for?

OpenAI Instrumentation

šŸ“œ Description

For chat completions, the OpenAI instrumentation records sampling parameters such as gen_ai.request.temperature, but does not record three request attributes defined by the OpenTelemetry GenAI semantic conventions, even when the request sets them:

Request parameter Semconv attribute Requirement level Recorded?
seed gen_ai.request.seed Conditionally Required (if set) no
response_format gen_ai.output.type Conditionally Required (if set) no
stop gen_ai.request.stop_sequences Recommended no

No other attribute carries these values either, so they are lost from the trace.

Impact: these parameters change model output (determinism, output format, where generation stops). Traces used for debugging, replay ("open in playground") or building eval datasets cannot reproduce such requests; e.g. a replayed response_format request returns free text instead of JSON.

For comparison, opentelemetry-instrumentation-genai-openai records all three (gen_ai.output.type = "json" for a json_schema response format).

šŸ‘Ÿ Reproduction steps

Self-contained (mock HTTP transport, no API key):

import json, httpx, openai
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.instrumentation.openai import OpenAIInstrumentor

exporter = InMemorySpanExporter()
provider = TracerProvider(); provider.add_span_processor(SimpleSpanProcessor(exporter))
OpenAIInstrumentor().instrument(tracer_provider=provider)

def handler(request):
    return httpx.Response(200, json={"id": "c1", "object": "chat.completion", "created": 0, "model": "m",
        "choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "{}"}}],
        "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}})
client = openai.OpenAI(api_key="x", base_url="http://mock/v1",
                       http_client=httpx.Client(transport=httpx.MockTransport(handler)))

client.chat.completions.create(
    model="m", messages=[{"role": "user", "content": "hi"}],
    seed=4242, stop=["ZZSTOP"],
    response_format={"type": "json_schema", "json_schema": {"name": "S", "schema": {"type": "object"}}},
    temperature=0.37,
)
a = exporter.get_finished_spans()[0].attributes
for k in ["gen_ai.request.seed", "gen_ai.request.stop_sequences", "gen_ai.output.type", "gen_ai.request.temperature"]:
    print(f"{k:32s} = {a.get(k)}")
print("any attribute mentioning seed/stop/output:",
      {k: v for k, v in a.items() if any(w in k for w in ("seed", "stop", "output.type", "response_format"))})

šŸ‘ Expected behavior

gen_ai.request.seed              = 4242
gen_ai.request.stop_sequences    = ('ZZSTOP',)
gen_ai.output.type               = json
gen_ai.request.temperature       = 0.37

šŸ‘Ž Actual Behavior with Screenshots

Same on 0.62.4 and on current main:

gen_ai.request.seed              = None
gen_ai.request.stop_sequences    = None
gen_ai.output.type               = None
gen_ai.request.temperature       = 0.37
any attribute mentioning seed/stop/output: {}

šŸ¤– Python Version

3.12

šŸ“ƒ Provide any additional context for the Bug.

  • opentelemetry-instrumentation-openai 0.62.4 and current main; openai 2.54.0; opentelemetry-sdk 1.45.0; Ubuntu 24.04.
  • Semantic conventions reference: GenAI spans, v1.41.0 (gen_ai.request.seed and gen_ai.output.type Conditionally Required; gen_ai.request.stop_sequences Recommended).
  • For comparison, opentelemetry-instrumentation-genai-openai 1.2b0 on the same script:
gen_ai.request.seed              = 4242
gen_ai.request.stop_sequences    = ('ZZSTOP',)
gen_ai.output.type               = json
gen_ai.request.temperature       = 0.37

šŸ‘€ Have you spent some time to check if this bug has been raised before?

  • [x] I checked and didn't find similar issue

Are you willing to submit PR?

Yes I am willing to submit a PR!

armanunix avatar Oct 01 '26 17:10 armanunix

The Anthropic instrumentation has the same gap for gen_ai.request.stop_sequences: stop_sequences is sent in the request but never recorded on the span. span_utils.set_input_attributes sets model, max_tokens, temperature, top_p, frequency_penalty and presence_penalty, but not stop_sequences.

Minimal check (mock transport, no API key), opentelemetry-instrumentation-anthropic 0.62.4, anthropic 1.11.0:

client.messages.create(model="m", max_tokens=10, stop_sequences=["END"], messages=[{"role": "user", "content": "hi"}])

stop_sequences in the request: ['END'] gen_ai.request.* on the span: {'gen_ai.request.model': 'm', 'gen_ai.request.max_tokens': 10}

PR #4548 covers the OpenAI package only, so it may be worth handling both there or in a follow-up.

(Side note: in anthropic 1.x, temperature, top_p and top_k are no longer parameters of messages.create, so stop_sequences is now the main sampling-related request attribute left to record for this SDK.)

armanunix avatar Oct 04 '26 15:10 armanunix

Hi! PR #4548 covers the OpenAI side. I’d like to take the Anthropic follow-up @armanunix pointed out: record gen_ai.request.stop_sequences in aset_input_attributes, plus a test with a mocked transport. Let me know if anyone is already on it.

Zzz23hao avatar Oct 14 '26 17:10 Zzz23hao