fix(openai): stop dropping cost when usage is a dict or cost is an int#1781
Open
arthi-arumugam-git wants to merge 1 commit into
Open
Conversation
_parse_cost only read `cost` off an attribute and only accepted `float`, so two real shapes were silently discarded: - `usage` arriving as a dict. `_parse_usage` already branches on `isinstance(usage, dict)` and both functions are called with the same object in `_extract_streamed_response_api_response`, so a dict reaches `_parse_cost` too. `hasattr(dict, "cost")` is always False. - an integer cost. `isinstance(0, float)` is False, so an OpenRouter `"cost": 0` from a free or fully cached model was dropped. Reads the key either way and accepts int as well as float. bool is excluded explicitly since it is a subclass of int.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_parse_costinlangfuse/openai.pydiscards cost in two shapes that occur in practice, socost_detailscomes backNoneand the invocation is recorded with no cost.1.
usageas a dict.hasattr({"cost": 0.002}, "cost")isFalse, so cost is dropped entirely. This is not hypothetical:_parse_usageright above it already branches onisinstance(usage, dict), and_update_langfuse_generationcalls both with the same object:So any dict-shaped usage that
_parse_usagehandles correctly loses its cost.2. An integer cost.
isinstance(0, float)isFalsein Python. OpenRouter reports"cost": 0for free models and for fully cached responses, and any whole-number cost serialises as an int. Those get dropped too.Measured against the current code, only one of five realistic shapes survives:
Usage(cost=0.0021){"total": 0.0021}{"total": 0.0021}Usage(cost=0)None{"total": 0.0}Usage(cost=2)None{"total": 2.0}{"cost": 0.0021}None{"total": 0.0021}{"cost": 0}None{"total": 0.0}Change
Read
costoff either shape, and acceptintas well asfloat.boolis excluded explicitly because it is a subclass ofint, so a stray"cost": truewould otherwise be recorded as a cost of1.0.Tests
tests/unit/test_parse_cost.py, 8 cases. Two of them fail on the current code and pass with the change:The other six cover
Noneusage, an object withoutcost, a dict withoutcost, and the bool and string rejections, so the fix cannot widen the accepted set by accident.ruff format --checkis clean on both files. The pre-existingruff checkfindings inopenai.pyare untouched.Greptile Summary
This PR fixes OpenRouter cost extraction and expands regression coverage.
Confidence Score: 5/5
The PR appears safe to merge with no actionable defects identified.
The parser now handles the documented usage shapes while preserving rejection of unsupported values, and the downstream cost-details contract accepts the normalized floating-point result.
Reviews (1): Last reviewed commit: "fix(openai): stop dropping cost when usa..." | Re-trigger Greptile
Context used: