🤖 fix: normalize mux-gateway file parts so image attachments are accepted - #4115
Open
Neppkun wants to merge 1 commit into
Open
🤖 fix: normalize mux-gateway file parts so image attachments are accepted#4115Neppkun wants to merge 1 commit into
Neppkun wants to merge 1 commit into
Conversation
Contributor
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
Attaching an image to a chat message on a
mux-gatewaymodel fails with "invalid request", while text-only turns work. The AI SDK v7 upgrade changed how@ai-sdk/gatewayserializes file parts on the wire, and the gateway server still validates the old shape. This PR adds a wire-level fetch shim that rewrites file parts back to the encoding the server accepts.Background
#3707 bumped
@ai-sdk/gatewayfrom 3.0.x (spec v3) to 4.0.15 (spec v4). The two versions encode inline file data differently:filepartdataon the wire"data:image/png;base64,iVBOR…"— plain string{ "type": "data", "data": "iVBOR…" }— tagged object (or{ "type": "url", "url": … })The request captured from a failing turn shows the v4 object shape, and the gateway server rejects it. (The server also still emits v2-style flat usage that
gatewayStreamNormalization.tspatches around, so it lagging the SDK's wire protocol is consistent.)Implementation
src/node/utils/gatewayFilePartNormalization.ts(new):wrapFetchWithGatewayFilePartNormalizationinspects gateway POST bodies and rewrites{type:"data"}→data:<mediaType>;base64,…(falling back toapplication/octet-stream, matching the 3.x SDK) and{type:"url"}→ the URL string. Covers user/assistantfileandreasoning-fileparts and files nested in tool-resultcontentoutput — the same set the SDK's ownmaybeEncodeFilePartstouches.inituntouched (cheap substring pre-check, no JSON round-trip);content-lengthis dropped when the body is rewritten.providerModelFactory.ts: wraps the mux-gatewaybaseFetchwith it as the innermost layer, so the existing Anthropic cache-control and auto-logout wrappers still apply on top.The module doc marks the shim as removable once the gateway server accepts spec-v4 file parts.
Validation
maybeEncodeFilePartsbetween@ai-sdk/gateway@3.0.110and@4.0.15.Risks
Low and scoped to
mux-gatewayrequests that contain file parts; text-only requests are byte-for-byte unchanged. If the gateway server is later upgraded to accept only the v4 shape, this shim would need to be removed — the module comment calls that out.Generated with
xum• Model:anthropic:claude-fable-5-1• Thinking:medium• Cost:$4.15