Repository navigation
Conversation
Reviewer's GuideImplementationTreat missing and null usage payloads as empty mappings in the Cohere chat response parser.
Treat missing and null usage payloads as empty mappings in LiteLLM chat and embedding response parsers.
Treat missing and null usage payloads as empty mappings in Mistral chat and embedding response parsers.
Treat missing and null usage payloads as empty mappings in the Sarvam chat response parser.
TestsAdd regression tests confirming all four chat parsers accept responses with null usage.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoHandle null usage in four provider response parsers
AI Description
Merge Risk
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can turn these tips off under Display > Comment parts |
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/instrumentation/cohere/utils.py" line_range="660" />
<code_context>
scope._response_id = response_dict.get("id")
scope._response_model = request_model
- usage = response_dict.get("usage", {})
+ usage = response_dict.get("usage") or {}
billed = usage.get("billed_units", {})
# Handle token usage including reasoning tokens and cached tokens
</code_context>
<issue_to_address>
**Null usage aborts Cohere streams**
When a Cohere streaming `message-end` event has `delta.usage` set to null, `process_chunk` receives `None` for `usage`, so `usage.get("billed_units", {})` raises `AttributeError`; the stream wrapper rethrows it, interrupts iteration, and marks the span ERROR.
Normalize `delta.get("usage")` to an empty mapping before accessing its fields.
Also at `sdk/python/src/openlit/instrumentation/cohere/utils.py:661`.
</issue_to_address>
### Comment 2
<location path="sdk/python/tests/test_null_usage.py" line_range="106" />
<code_context>
+def test_litellm_chat_null_usage_does_not_crash():
+ from openlit.instrumentation.litellm.utils import process_chat_response
+
+ _run(process_chat_response, dict(OPENAI_SHAPED_RESPONSE))
+
+
</code_context>
<issue_to_address>
**Sarvam regression test passes unchanged**
When the Sarvam null-usage test runs, `process_chat_response` skips token extraction when `usage` is falsey, and its subsequent fallback is already null-safe, so this test passes on both the pre-PR and changed code and does not verify a Sarvam regression fix.
Remove Sarvam from the claimed regression cases or replace it with a scenario that fails before the change and assert the intended behavior.
</issue_to_address>
### Comment 3
<location path="sdk/python/tests/test_null_usage.py" line_range="88" />
<code_context>
+def test_litellm_chat_null_usage_does_not_crash():
+ from openlit.instrumentation.litellm.utils import process_chat_response
+
+ _run(process_chat_response, dict(OPENAI_SHAPED_RESPONSE))
+
+
</code_context>
<issue_to_address>
**No-op parsers pass the tests**
When a parser silently skips response processing instead of raising, a parser that returns without processing the response completes each bare `_run(...)` call, so all four tests pass even though no response telemetry is recorded.
Assert that the response is processed successfully and that the span records the expected telemetry.
Also at `sdk/python/tests/test_null_usage.py:94`, `sdk/python/tests/test_null_usage.py:100`, `sdk/python/tests/test_null_usage.py:106`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and a null usage field is now treated as an empty object, so the parser records zero token usage and continues emitting response telemetry instead of raising an exception. If zero is misleading, those exported metrics may remain in the monitoring backend after a revert, though the effect is bounded to responses with null usage.
Blocking findings: sdk/python/src/openlit/instrumentation/cohere/utils.py:660, sdk/python/tests/test_null_usage.py:106, sdk/python/tests/test_null_usage.py:88
| scope._response_id = response_dict.get("id") | ||
| scope._response_model = request_model | ||
| usage = response_dict.get("usage", {}) | ||
| usage = response_dict.get("usage") or {} |
There was a problem hiding this comment.
🟡 Medium · Null usage aborts Cohere streams
When a Cohere streaming message-end event has delta.usage set to null, process_chunk receives None for usage, so usage.get("billed_units", {}) raises AttributeError; the stream wrapper rethrows it, interrupts iteration, and marks the span ERROR.
Normalize delta.get("usage") to an empty mapping before accessing its fields.
Also at sdk/python/src/openlit/instrumentation/cohere/utils.py:661.
Prompt for AI agents
In `sdk/python/src/openlit/instrumentation/cohere/utils.py` at line 660:
**Null usage aborts Cohere streams**
When a Cohere streaming `message-end` event has `delta.usage` set to null, `process_chunk` receives `None` for `usage`, so `usage.get("billed_units", {})` raises `AttributeError`; the stream wrapper rethrows it, interrupts iteration, and marks the span ERROR.
Normalize `delta.get("usage")` to an empty mapping before accessing its fields.
Also at `sdk/python/src/openlit/instrumentation/cohere/utils.py:661`.| def test_sarvam_chat_null_usage_does_not_crash(): | ||
| from openlit.instrumentation.sarvam.utils import process_chat_response | ||
|
|
||
| _run(process_chat_response, dict(OPENAI_SHAPED_RESPONSE)) |
There was a problem hiding this comment.
🟡 Medium · Sarvam regression test passes unchanged
When the Sarvam null-usage test runs, process_chat_response skips token extraction when usage is falsey, and its subsequent fallback is already null-safe, so this test passes on both the pre-PR and changed code and does not verify a Sarvam regression fix.
Remove Sarvam from the claimed regression cases or replace it with a scenario that fails before the change and assert the intended behavior.
Prompt for AI agents
In `sdk/python/tests/test_null_usage.py` at line 106:
**Sarvam regression test passes unchanged**
When the Sarvam null-usage test runs, `process_chat_response` skips token extraction when `usage` is falsey, and its subsequent fallback is already null-safe, so this test passes on both the pre-PR and changed code and does not verify a Sarvam regression fix.
Remove Sarvam from the claimed regression cases or replace it with a scenario that fails before the change and assert the intended behavior.| def test_litellm_chat_null_usage_does_not_crash(): | ||
| from openlit.instrumentation.litellm.utils import process_chat_response | ||
|
|
||
| _run(process_chat_response, dict(OPENAI_SHAPED_RESPONSE)) |
There was a problem hiding this comment.
🟡 Medium · No-op parsers pass the tests
When a parser silently skips response processing instead of raising, a parser that returns without processing the response completes each bare _run(...) call, so all four tests pass even though no response telemetry is recorded.
Assert that the response is processed successfully and that the span records the expected telemetry.
Also at sdk/python/tests/test_null_usage.py:94, sdk/python/tests/test_null_usage.py:100, sdk/python/tests/test_null_usage.py:106.
Prompt for AI agents
In `sdk/python/tests/test_null_usage.py` at line 88:
**No-op parsers pass the tests**
When a parser silently skips response processing instead of raising, a parser that returns without processing the response completes each bare `_run(...)` call, so all four tests pass even though no response telemetry is recorded.
Assert that the response is processed successfully and that the span records the expected telemetry.
Also at `sdk/python/tests/test_null_usage.py:94`, `sdk/python/tests/test_null_usage.py:100`, `sdk/python/tests/test_null_usage.py:106`.
Summary
The litellm, mistral, cohere, and sarvam instrumentations crash with
AttributeError: 'NoneType' object has no attribute 'get'when a provider response carries"usage": null, marking the span ERROR and dropping all response telemetry. This is the same defect reported for Azure AI Inference in #2023 and AI21 in #2022:response_dict.get("usage", {})does not help when theusagekey is present with aNonevalue, which gateways and provider outages emit in the wild for this OpenAI-shaped response contract.Changes
litellm,mistral,cohere,sarvam: parse usage withresponse_dict.get("usage") or {}in the chat and embedding response paths, matching the pattern already used in the OpenAI instrumentation.tests/test_null_usage.pyfeed"usage": Noneresponses to all four chat parsers.Red/green verified: all four tests fail on
mainwith the exactAttributeErrorabove and pass with the fix. Groq and the litellm streaming path were checked and are already guarded (isinstance/ truthiness checks), so they are untouched. AI21 and Azure AI Inference are intentionally excluded since #2024/#2026 already cover them.Test plan
pytest tests/test_null_usage.py— 4/4 pass (4/4 fail without the fix)Summary by Sourcery
Handle null usage payloads across supported provider response parsers without interrupting instrumentation.
Bug Fixes:
Tests: