Repository navigation
Conversation
_set_response_attributes did dict(usage.get('prompt_tokens_details', {}));
the openai SDK carries the key with a None value when the response omits
the optional field, so dict(None) raised TypeError (swallowed by
@dont_throw), and gen_ai.usage.cache_read.input_tokens was never set.
Treat a missing/None value as empty so the attribute is recorded as 0.
Adds mock-transport regression tests covering sync, streaming (with
stream_options usage), and async paths, plus the cached-tokens-present
behavior pin.
Fixes traceloop#4583
chrikrah
left a comment
There was a problem hiding this comment.
@rupak-eng the or {} line in shared/__init__.py is right. The new test module breaks five existing tests in this package, so it needs one change before merge.
blocking: the local span_exporter fixture builds and tears down its own OpenAIInstrumentor(), and that leaves global state behind for test_vision.py, which runs later. The constructor overwrites Config.upload_base64_image, so test_vision_base64 fails on a KeyError. The other four fail on assert 10 == 2 over the log records. Restoring Config after the fixture fixes only the first one.
$ pytest tests -q -p no:sugar # head 46b5ffa, Python 3.10, openai 1.99.7
FAILED tests/traces/test_vision.py::test_vision_base64 - KeyError: 'gen_ai.in...
5 failed, 290 passed, 1 skipped
$ pytest tests -q -p no:sugar # base df4921a
291 passed, 1 skipped
Replace the local span_exporter fixture with the shared one, as test_responses.py:1014 does:
@pytest.fixture(autouse=True)
def _instrumented(instrument_legacy):
yield$ pytest tests -q -p no:sugar # head, local fixture replaced
295 passed, 1 skipped
$ pytest tests/traces/test_usage_cache_read_tokens.py -q # same, `or {}` reverted
3 failed, 1 passed
After that change, nothing uses the TracerProvider, SimpleSpanProcessor and InMemorySpanExporter imports.
non-blocking: when input_tokens_details is absent, responses_wrappers.py:490 omits gen_ai.usage.cache_read.input_tokens. This path records 0.
@doronkopit5 should a missing prompt_tokens_details record 0 here, or be omitted like the Responses path?
Fixes #4583
_set_response_attributesdiddict(usage.get("prompt_tokens_details", {})). The openai SDK carries the key with aNonevalue when the response omits the optional field, sodict(None)raisedTypeError(swallowed by@dont_throw), andgen_ai.usage.cache_read.input_tokenswas never set. A missing/Nonevalue is now treated as empty, so the attribute is recorded as 0 — mirroring thecompletion_tokens_detailsNoneguard inchat_wrappers.py.Testing
httpx.MockTransportharness:gen_ai.usage.cache_read.input_tokenswasMISSINGbefore,0after (no API key/network).tests/traces/test_usage_cache_read_tokens.py: 4 mock-based tests — sync, streaming (stream_optionsusage), async withprompt_tokens_details=None→ attribute recorded as 0; plus a pin that a presentcached_tokensvalue still records correctly. 3 of the 4 fail on unpatched code (the 4th pins pre-existing behavior).tests/traces/test_semconv_compliance.py: 34/34 pass with the change.ruff checkclean on both files.Summary by CodeRabbit