fix(openai): @dont_throw is a no-op on Responses API wrappers, so tracing bugs crash host apps
Summary
In packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py, the Responses API wrappers are decorated like this:
@dont_throw
@_with_tracer_wrapper
def responses_get_or_create_wrapper(tracer, wrapped, instance, args, kwargs): ...In this order, @dont_throw protects nothing. _with_tracer_wrapper(func) returns _with_tracer(tracer), a factory that only builds the wrapt wrapper closure. So dont_throw only guards building that closure, not the code that runs on every API call. _with_tracer is also not a coroutine function, so the async variants get dont_throw's sync branch as well.
The result: any bug in our tracing code in these wrappers propagates into the host application instead of being logged. #4476 was exactly this. An AttributeError raised while building the span data crashed Microsoft Agent Framework.
Affected (on main):
| Line | Function |
|---|---|
responses_wrappers.py:483 |
responses_get_or_create_wrapper |
responses_wrappers.py:652 |
async_responses_get_or_create_wrapper |
responses_wrappers.py:820 |
responses_cancel_wrapper |
responses_wrappers.py:850 |
async_responses_cancel_wrapper |
Context
- Came up while reviewing #4477 (the fix for #4476). #4477 removes that one crash, but the protection meant to stop any tracing error from reaching the caller is missing, so the next bug will surface the same way.
- Related earlier crash in the same wrappers: #4058 / #4078.
Reproduction
From packages/opentelemetry-instrumentation-openai:
uv run python -c "
from opentelemetry.instrumentation.openai.utils import dont_throw, _with_tracer_wrapper
@dont_throw
@_with_tracer_wrapper
def w(tracer, wrapped, instance, args, kwargs):
raise RuntimeError('instrumentation bug')
try:
w(None)(lambda: 1, None, (), {})
print('swallowed')
except RuntimeError as e:
print('PROPAGATED:', e)
"Expected: swallowed. The error is logged at debug level, and Config.exception_logger gets it if one is set.
Actual: PROPAGATED: instrumentation bug.
End-to-end: check out main from before #4477 and run the two *_with_raw_response_streaming_does_not_crash tests that #4477 adds. The AttributeError reaches the test instead of being swallowed.
Why just swapping the decorator order isn't enough
If the order were @_with_tracer_wrapper then @dont_throw, the whole wrapper body would be protected, including the call to wrapped(*args, **kwargs). But dont_throw returns None on any exception, which causes two new problems:
- A tracing bug would make
client.responses.create(...)returnNoneto the user (still broken, just less obviously). - Real OpenAI errors raised by
wrapped()(e.g.RateLimitError) would be swallowed instead of re-raised, and the wrapper currently re-raises them on purpose after recording them on the span.
Suggested fix
- Separate the user's call from our telemetry code. Call
wrapped()exactly once, outside any swallowing, and let its exceptions pass through. Run everything after it (parsing, buildingTracedData, starting and ending spans) insidetry/except Exceptionthat logs through the same path asdont_throwand thenreturn response. The sync wrapper already does this partly (except Exception: return responsearound trace assembly), butparse_response(...)andresponses.get(parsed_response.id)sit outside that block. Those are the lines that crashed in #4058 and #4476. - Remove the no-op
@dont_throwfrom these 4 functions, or move it onto small helpers that contain only telemetry code, so it doesn't look like protection. - Add a regression test: monkeypatch a helper such as
parse_responseorset_data_attributesto raise, callclient.responses.create(...)(a mocked httpx transport is enough, see the tests in #4477), and check that the real response comes back and no exception reaches the caller. Add the async version too. - Check the other packages for the same
@dont_throwabove@_with_tracer_wrapperorder. Onmain, only these 4 functions have it.
Source: traceloop/openllmetry