Repository navigation
fix(ai21): accept null usage in chat and RAG parsers - #2026
Conversation
Reviewer's GuideImplementationTreat null usage values as empty dictionaries in both chat and RAG response parsers so token extraction does not fail.
TestsAdd sync and async regression tests for null usage in chat and RAG responses, plus a valid-usage behavior check.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by Qodofix(ai21): accept null usage in chat and RAG responses
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 tweak Display settings with a live preview to see your comment before it ships |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/instrumentation/ai21/utils.py" line_range="667" />
<code_context>
scope._response_model = response_dict.get("model") or request_model
# Handle token usage including reasoning tokens and cached tokens
- usage = response_dict.get("usage", {})
+ usage = response_dict.get("usage") or {}
scope._input_tokens = usage.get("prompt_tokens", 0)
scope._output_tokens = usage.get("completion_tokens", 0)
</code_context>
<issue_to_address>
**Unavailable usage is recorded as zero**
When AI21 returns `usage: null` for a response that consumed tokens, `process_chat_response` defaults missing prompt and completion counts to zero, and `common_chat_logic` treats those attributes as present instead of estimating from the response; the span therefore records zero tokens and cost for a real completion. The RAG path likewise records unavailable cache counts as zero, so callers see false usage data.
When usage is unavailable, estimate token counts from the request and completion (and represent unavailable cache counts distinctly where possible) instead of recording them as zero.
Also at `sdk/python/src/openlit/instrumentation/ai21/utils.py:906`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the fallback is wrong, affected calls will produce spans with zero token counts instead of preserving the prior failed-span behavior. Those exported telemetry records cannot be changed by reverting, though the impact is bounded to the affected calls.
Blocking findings: sdk/python/src/openlit/instrumentation/ai21/utils.py:667
| scope._response_model = response_dict.get("model") or request_model | ||
| # Handle token usage including reasoning tokens and cached tokens | ||
| usage = response_dict.get("usage", {}) | ||
| usage = response_dict.get("usage") or {} |
There was a problem hiding this comment.
🟡 Medium · Unavailable usage is recorded as zero
When AI21 returns usage: null for a response that consumed tokens, process_chat_response defaults missing prompt and completion counts to zero, and common_chat_logic treats those attributes as present instead of estimating from the response; the span therefore records zero tokens and cost for a real completion. The RAG path likewise records unavailable cache counts as zero, so callers see false usage data.
When usage is unavailable, estimate token counts from the request and completion (and represent unavailable cache counts distinctly where possible) instead of recording them as zero.
Also at sdk/python/src/openlit/instrumentation/ai21/utils.py:906.
Prompt for AI agents
In `sdk/python/src/openlit/instrumentation/ai21/utils.py` at line 667:
**Unavailable usage is recorded as zero**
When AI21 returns `usage: null` for a response that consumed tokens, `process_chat_response` defaults missing prompt and completion counts to zero, and `common_chat_logic` treats those attributes as present instead of estimating from the response; the span therefore records zero tokens and cost for a real completion. The RAG path likewise records unavailable cache counts as zero, so callers see false usage data.
When usage is unavailable, estimate token counts from the request and completion (and represent unavailable cache counts distinctly where possible) instead of recording them as zero.
Also at `sdk/python/src/openlit/instrumentation/ai21/utils.py:906`.
Merged PR summary
Generated from the merged PR description and bounded, component-scoped file evidence. Release preparation reuses this validated summary. |
Issue number: #2022
Change description:
process_chat_responseandprocess_chat_rag_responsecalled.get()onresponse_dict.get("usage", {}); explicit JSONnullreturnedNoneand raisedAttributeError, marking the span ERROR. Changed both sites toget("usage") or {}; valid-usage behavior is untouched.Checklist
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Verification
New null-usage tests across chat/chat_rag and sync/async wrappers: base-red 4 failed / 1 passed (
StatusCode.ERRORinstead of OK); with fix 5/5 passed. Related stream-lifecycle module 16/16 passed.ast.parseclean; ruff findings on the source file are pre-existing.No live provider calls, network, or credentials — all responses are synthetic dicts.
Summary by Sourcery
Prevent null AI21 usage fields from causing response parsing failures and incomplete telemetry.
Bug Fixes:
Tests: