fix(api): strip MCP image base64 from tool results in the jinja path (#2374) - #2376
Merged
LostRuins merged 1 commit intoAug 2, 2026
Conversation
…ostRuins#2374) When a tool/MCP result carries an image, the OpenAI-compatible chat adapter's jinja code path left the base64 payload in the rendered prompt as plain text (a single 1024x1024 jpeg bloated the context by ~120k tokens), while the legacy path already stripped it via strip_mcpcontent_of_media. - format_jinja now strips the base64 from tool-role string content before rendering, matching the legacy path; the image itself is still swept out and attached separately. - sweep_media_from_messages now also recognizes MCP-style image content blocks (type == "image") inside a content list, so images delivered that way are attached instead of dropped.
LostRuins
approved these changes
Aug 2, 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.
Problem
Fixes #2374. When a tool / MCP result carries an image, the OpenAI-compatible chat adapter's jinja code path leaves the base64 payload in the rendered prompt as plain text. As reported, a single 1024×1024 jpeg bloats the context by ~120k tokens. The legacy (non-jinja) path already handles this correctly via
strip_mcpcontent_of_media, so the behavior is inconsistent between the two paths.There are two related gaps:
format_jinjanever strips the base64 from tool-role string content before rendering, so the whole MCP JSON (including the base64) is templated as text.sweep_media_from_messagesonly recognizesimage_urlitems inside a content list, not MCP-styleimagecontent blocks ({"type": "image", "data": ...}), so images delivered that way are dropped instead of attached.Fix
format_jinja: strip the base64 from tool-role string content before rendering (reusing the existingstrip_mcpcontent_of_media). The image itself is still swept out and attached separately, so nothing is lost — only the multi-KB base64 stops being rendered as text.sweep_media_from_messages: also recognizetype == "image"content blocks in a list and attach theirdata.Verification
Reproduced both paths with a standalone harness driving the jinja template + sweep logic:
imageblocksChange is 7 added lines, no deletions, and mirrors the existing legacy-path behavior.
🤖 Generated with Claude Code