fix: cost for image generation or image edit streaming - #4802
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughSummary by CodeRabbit
WalkthroughUpdates image streaming completion handling to use streamed usage data directly, backfill missing image counts, and adjust image response schema serialization and backfill behavior. ChangesImage Streaming Usage Fix
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Comment |
28eccba to
d22f092
Compare
d22f092 to
7ac0adc
Compare
7ac0adc to
28a3c79
Compare
The merge-base changed after approval.
28a3c79 to
a5285a5
Compare
Merge activity
|
* 'dev' of https://github.com/maximhq/bifrost: ipv6 support (maximhq#4895) docs: add virtual key expiry support docs (maximhq#4889) test: add Postman e2e collection and runner for virtual key expiry validation and enforcement (maximhq#4888) feat: add expiry field to virtual keys (maximhq#4887) fix: converts thinking to disabled if tool choice is required for deepseek (maximhq#4861) chore: adds docs for deepseek provider (maximhq#4854) chore: adds tests for deepseek provider (maximhq#4853) feat: adds deepseek provider (maximhq#4852) fix: cost for image generation or image edit streaming (maximhq#4802) feat: add `BedrockMantleKeyConfig` support to key hashing, schema/table mapping, and sensitive field clearing (maximhq#4886) fix: skip O(N) reference refresh on request-time rate-limit/budget reset (maximhq#4883) refactor: simplify Responses lifecycle permissions to require explicit per-verb flags and expose them in UI (maximhq#4880) fix: append datasheet models for incomplete list models call (maximhq#4879) # Conflicts: # ui/app/workspace/providers/fragments/allowedRequestsFields.tsx # ui/app/workspace/virtual-keys/views/virtualKeyDetailsSheet.tsx # ui/app/workspace/virtual-keys/views/virtualKeySheet.tsx # ui/app/workspace/virtual-keys/views/virtualKeysTable.tsx # ui/components/ui/datePickerWithRange.tsx
## Summary Eliminates the intermediate `collectedUsage` variable in image generation and image edit streaming handlers, instead using `response.Usage` directly on the completed event. Also preserves any `OutputTokensDetails` already present in the response rather than unconditionally overwriting it, and hides `NumInputImages` from JSON serialization. ## Changes - Removed the `collectedUsage` accumulator variable from both `HandleOpenAIImageGenerationStreaming` and `HandleOpenAIImageEditStreamRequest`. Usage data is now read directly from `response.Usage` when the completed event is received, avoiding an unnecessary copy. - Updated the `NImages` backfill logic to only initialize `OutputTokensDetails` if it is `nil`, and only set `NImages` if it is not already populated — preserving any details the provider may have already returned. - Changed `NumInputImages` in `ImageUsage` to use `json:"-"` so it is no longer included in serialized responses (it is an internal Bifrost field). - Changed `ExtraFields` in `BifrostImageGenerationResponse` and `BifrostImageGenerationStreamResponse` to always serialize (`json:"extra_fields"` instead of `json:"extra_fields,omitempty"`). - Removed the unused `getModelFromRequest` helper function from `schemas/images.go`. Closes maximhq#4777 ## Type of change - [x] Refactor - [x] Bug fix ## Affected areas - [x] Core (Go) - [x] Providers/Integrations ## How to test ```sh go test ./... ``` Trigger a streaming image generation and image edit request and verify that: - Usage fields are correctly populated on the completed chunk. - `NImages` reflects the number of images generated. - `num_input_images` no longer appears in the JSON response. - `extra_fields` is always present in the response body. ## Breaking changes - [x] No ## Related issues ## Security considerations None. ## Checklist - [x] I read `docs/contributing/README.md` and followed the guidelines - [x] I added/updated tests where appropriate - [x] I updated documentation where needed - [x] I verified builds succeed (Go and UI) - [x] I verified the CI pipeline passes locally if applicable

Summary
Eliminates the intermediate
collectedUsagevariable in image generation and image edit streaming handlers, instead usingresponse.Usagedirectly on the completed event. Also preserves anyOutputTokensDetailsalready present in the response rather than unconditionally overwriting it, and hidesNumInputImagesfrom JSON serialization.Changes
collectedUsageaccumulator variable from bothHandleOpenAIImageGenerationStreamingandHandleOpenAIImageEditStreamRequest. Usage data is now read directly fromresponse.Usagewhen the completed event is received, avoiding an unnecessary copy.NImagesbackfill logic to only initializeOutputTokensDetailsif it isnil, and only setNImagesif it is not already populated — preserving any details the provider may have already returned.NumInputImagesinImageUsageto usejson:"-"so it is no longer included in serialized responses (it is an internal Bifrost field).ExtraFieldsinBifrostImageGenerationResponseandBifrostImageGenerationStreamResponseto always serialize (json:"extra_fields"instead ofjson:"extra_fields,omitempty").getModelFromRequesthelper function fromschemas/images.go.Closes #4777
Type of change
Affected areas
How to test
go test ./...Trigger a streaming image generation and image edit request and verify that:
NImagesreflects the number of images generated.num_input_imagesno longer appears in the JSON response.extra_fieldsis always present in the response body.Breaking changes
Related issues
Security considerations
None.
Checklist
docs/contributing/README.mdand followed the guidelines