fix(ai-gateway): always rewrite proxied model responses - #4851
Merged
Conversation
Remove the passthrough shortcut in rewriteModelResponse so every response is processed: stream errors are common and were undebuggable for the traffic that skipped the rewrite (paid models without request logging), and processing the full stream is the only way to surface them. rewriteModelResponse now always returns a NextResponse and throws on an unrecognized API kind instead of returning null.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryReviewed the removal of the passthrough shortcut in Files Reviewed (4 files)
Reviewed by claude-sonnet-5 · Input: 14 · Output: 2.2K · Cached: 249.8K Review guidance: REVIEW.md from base branch |
marius-kilocode
approved these changes
Jul 29, 2026
This was referenced Jul 29, 2026
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.
Summary
Removes the passthrough shortcut in
rewriteModelResponseso every proxied model response goes through the processing pipeline (previously only Kilo-exclusive free models and request-logged traffic did).Why
Stream errors (upstream timeouts/disconnects) are common, and for the traffic that skipped the rewrite — paid models without request logging — they were undebuggable: the raw stream was passed through untouched, so mid-stream failures surfaced only as connection errors with no captured context. Processing the full stream is the only way to surface and log them.
Performance is not a concern here: token generation is inherently slow, and ~50% of traffic (free models + internal traffic with logging enabled) already goes through the rewrite.
Changes
rewriteModelResponsenow always returns aNextResponse(nevernull); the route returns its result directly instead of falling back towrapInSafeNextResponse.wrapInSafeNextResponse, and the removed skip-behavior test now asserts paid-model traffic is rewritten with cost preserved.Verification
rewriteModelResponse.test.ts: 58/58 passopenrouter/[...path]/route.test.ts: 20/20 passtsgo --noEmit(web): cleanoxlinton the 4 changed files: 0 warnings, 0 errorspnpm format:changedapplied;git diff --checkclean