Bug Report: penAI instrumentation consumes generator/iterator `messages` and `tools`, so the model receives an empty conversation (default settings)
Which component is this bug for?
OpenAI Instrumentation
Description
The OpenAI SDK accepts messages and tools as any iterable, including generators and map() objects, and sends them correctly. With the OpenAI instrumentation active (default settings), these one-shot iterables are consumed by the instrumentation while recording content, and the SDK then sends an empty list to the API:
messages=(m for m in history)→ the request contains 0 messagesmessages=map(...)→ 0 messagestools=(t for t in tools)→ the request contains 0 tools- same for
AsyncOpenAI
Meanwhile the span records the full messages and tools, so the trace shows a conversation the model never received.
Impact:
- Empty
messages: the real API rejects the request (400), but only when tracing is on; disabling tracing to debug makes the error disappear. - Empty
tools: the request is valid, so the model silently answers without access to its tools: no error, changed agent behavior, and the trace still lists the tools.
The consumption happens in the content-recording path: with TRACELOOP_TRACE_CONTENT=false the requests are correct. Since content recording is on by default, the default configuration is affected.
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)
received = []
def handler(request):
body = json.loads(request.content)
received.append({"messages": len(body.get("messages", [])), "tools": len(body.get("tools", []))})
return httpx.Response(200, json={"id": "c1", "object": "chat.completion", "created": 0, "model": "m",
"choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "ok"}}],
"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)))
MSGS = [{"role": "system", "content": "be brief"}, {"role": "user", "content": "hi"}]
TOOLS = [{"type": "function", "function": {"name": "get_weather", "parameters": {"type": "object"}}}]
client.chat.completions.create(model="m", messages=(m for m in MSGS))
print("messages generator -> server received:", received[-1], "(expected 2 messages)")
client.chat.completions.create(model="m", messages=MSGS, tools=(t for t in TOOLS))
print("tools generator -> server received:", received[-1], "(expected 1 tool)")Expected behavior
The request sent to the API is identical with and without instrumentation: 2 messages and 1 tool. The instrumentation should not consume one-shot iterables, e.g. by materializing them once (list(...)) and passing the materialized list on to the wrapped call.
Actual Behavior with Screenshots
Default settings (and TRACELOOP_TRACE_CONTENT=true):
messages generator -> server received: {'messages': 0, 'tools': 0} (expected 2 messages)
tools generator -> server received: {'messages': 2, 'tools': 0} (expected 1 tool)With TRACELOOP_TRACE_CONTENT=false:
messages generator -> server received: {'messages': 2, 'tools': 0} (expected 2 messages)
tools generator -> server received: {'messages': 2, 'tools': 1} (expected 1 tool)Also reproduced with messages=map(...) and with AsyncOpenAI.
Python Version
3.12
Provide any additional context for the Bug.
- opentelemetry-instrumentation-openai 0.62.4 and current
main(same result); openai 2.54.0; opentelemetry-sdk 1.45.0; Ubuntu 24.04. - OpenInference's OpenAI instrumentation handles the same inputs correctly (the full request is sent).
Have you spent some time to check if this bug has been raised before?
- I checked and didn't find similar issue
Are you willing to submit PR?
Yes I am willing to submit a PR!
Source: traceloop/openllmetry