Fix streamed message model_id latching onto empty string (nil costs on Azure OpenAI)#830
Merged
Merged
Conversation
Azure OpenAI's first stream chunk (the prompt filter chunk) carries "model": "". StreamAccumulator's `@model_id ||= chunk.model_id` treats the empty string as set, so the assembled message keeps an empty model_id, model lookup fails, and message.cost returns nil for every component despite valid token usage. Keep the first non-empty model_id instead. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #830 +/- ##
=======================================
Coverage 82.18% 82.18%
=======================================
Files 170 170
Lines 8029 8029
Branches 1347 1348 +1
=======================================
Hits 6599 6599
Misses 890 890
Partials 540 540 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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 this does
Fixes streamed messages losing their
model_id— and therefore all cost information — when the provider's first stream chunk carries an emptymodelfield.Azure OpenAI's first SSE chunk (the prompt filter chunk) has
"model": "", while subsequent chunks carry the real id:StreamAccumulator#adddoes@model_id ||= chunk.model_id, and since""is truthy, the empty string latches for the whole stream. The assembledMessagethen hasmodel_id: "",Message#model_inforaisesModelNotFoundErrorinternally, andmessage.costreturnsnilfor every component (total,input,output, …) even though token usage is fully populated. Non-streamed requests against the same endpoint price correctly.The fix keeps the first non-empty
model_idinstead:Verified against a live Azure OpenAI deployment: before the change a streamed
chat.askreturnedcost.total => nil; after, it resolves the dated model and prices identically to the non-streaming path.Type of change
Scope check
Quality check
bundle exec rspec(stream accumulator, streaming, and protocol specs)models.json,aliases.json)AI-generated code
API changes