feat(openai): trace responses.with_raw_response.create(stream=True) instead of skipping it
Summary
client.responses.with_raw_response.create(stream=True, ...) produces no span. This is what Microsoft Agent Framework (agent-framework-openai >= 1.6.0) uses so it can read the x-ms-served-model response header before streaming. So users of that framework currently get no traces for Responses API streaming.
Context
- #4476: this call used to crash with
AttributeError: 'AsyncStream' object has no attribute 'id'. - #4477 fixes the crash by skipping tracing. After
parse_response()/async_parse_response(), if the result is aStream/AsyncStream, the wrapper now returns the raw response untouched. The PR description calls this out as an intentional trade-off. This issue tracks actually tracing that path. - The same gap applies to
with_streaming_response.create(stream=True)andwith_raw_response.retrieve(..., stream=True). All of them go throughresponses_get_or_create_wrapper/async_responses_get_or_create_wrapperinpackages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py.
Reproduction
After #4477 is merged, from packages/opentelemetry-instrumentation-openai, add this test to tests/traces/test_responses.py. It uses a mocked httpx transport, so it needs no API key or cassette:
_RESPONSES_SSE_BODY = (
b'event: response.created\n'
b'data: {"type":"response.created","response":{"id":"resp_123","object":"response",'
b'"created_at":0,"status":"in_progress","model":"gpt-4.1-nano","output":[]}}\n\n'
b'event: response.completed\n'
b'data: {"type":"response.completed","response":{"id":"resp_123","object":"response",'
b'"created_at":0,"status":"completed","model":"gpt-4.1-nano","output":[],'
b'"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}\n\n'
)
def test_responses_with_raw_response_streaming_is_traced(instrument_legacy, span_exporter):
import httpx
from openai import OpenAI
def handler(request):
return httpx.Response(
200,
headers={"content-type": "text/event-stream", "x-ms-served-model": "gpt-4.1-nano"},
content=_RESPONSES_SSE_BODY,
)
client = OpenAI(api_key="test-key", http_client=httpx.Client(transport=httpx.MockTransport(handler)))
raw = client.responses.with_raw_response.create(
model="gpt-4.1-nano", input="What is the capital of France?", stream=True
)
assert raw.headers["x-ms-served-model"] == "gpt-4.1-nano" # raw contract must still hold
events = list(raw.parse())
assert len(events) == 2
spans = span_exporter.get_finished_spans()
assert len(spans) == 1 # FAILS today: 0 spans
assert spans[0].attributes["gen_ai.response.id"] == "resp_123"
uv run pytest tests/traces/test_responses.py -k with_raw_response_streaming_is_traced
Expected: one openai.response span with the usual request, response and usage attributes, same as client.responses.create(stream=True).
Actual: no spans. #4477 also adds tests that assert get_finished_spans() == (), so those need updating once this is fixed.
Add an async version with AsyncOpenAI + httpx.AsyncClient. There, raw.parse() returns an AsyncStream.
Suggested approach
Callers of .with_raw_response depend on getting a raw-response object back (.headers, .parse(), .http_response, ...). So we can't swap in ResponseStream directly the way the plain stream=True path does. Options:
- Wrap the raw response and hook
.parse()(preferred): whenwrapped()returns aLegacyAPIResponse/APIResponse/AsyncAPIResponseandstream=Trueis in kwargs, return anObjectProxyof it. Itsparse()calls the originalparse()and, if the result is aStream/AsyncStream, wraps it once inResponseStream(span=..., response=stream, ...)and caches that. Start the span at request time, like the direct streaming path does. Two things to watch:LegacyAPIResponse.parse()is sync even on the async client, whileAsyncAPIResponse.parse()is a coroutine.parse()caches perto=type, so the proxy should keep the same object identity across repeatedparse()calls.
- If the proxy can't be made reliable, at minimum emit a span with the request attributes and end it right away, so the call still shows up in the trace.
Tests: extend the two regression tests from #4477 to check the span attributes and that .headers still works before .parse().