Fix Anthropic image content normalization#8
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for normalizing Anthropic-style image blocks (with both URL and base64 sources) into OpenAI-compatible image_url objects, along with corresponding unit tests. The feedback suggests trimming whitespace from the base64 media_type and data fields before validation and URL construction to improve robustness against leading or trailing whitespace.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| mediaType, _ := source["media_type"].(string) | ||
| data, _ := source["data"].(string) | ||
| if strings.TrimSpace(mediaType) == "" || strings.TrimSpace(data) == "" { | ||
| return nil, fmt.Errorf("%s.source media_type and data are required for base64 images", path) | ||
| } | ||
| url = "data:" + mediaType + ";base64," + data |
There was a problem hiding this comment.
When constructing a data URL for base64-encoded images, any leading or trailing whitespace (such as newlines, which are very common in base64 payloads) in media_type or data can result in an invalid data URL. Trimming these values before validation and URL construction ensures robustness and compatibility with various client inputs.
| mediaType, _ := source["media_type"].(string) | |
| data, _ := source["data"].(string) | |
| if strings.TrimSpace(mediaType) == "" || strings.TrimSpace(data) == "" { | |
| return nil, fmt.Errorf("%s.source media_type and data are required for base64 images", path) | |
| } | |
| url = "data:" + mediaType + ";base64," + data | |
| mediaType, _ := source["media_type"].(string) | |
| data, _ := source["data"].(string) | |
| mediaType = strings.TrimSpace(mediaType) | |
| data = strings.TrimSpace(data) | |
| if mediaType == "" || data == "" { | |
| return nil, fmt.Errorf("%s.source media_type and data are required for base64 images", path) | |
| } | |
| url = "data:" + mediaType + ";base64," + data |
Motivation
type: "image"content in/v1/messagesso it no longer triggers unsupported-type errors during OpenAI chat preparation.image_urlparts and client-provided metadata is stripped/recorded as a change.Description
case "image"branch inprepareMessageContentto accept Anthropicimageblocks and map them toimage_urlparts (file:internal/openai/chat_prepare.go).prepareAnthropicImageto validatesourceobjects, supporturlandbase64sources (constructingdata:<media_type>;base64,<data>), and record the rewrite withrecordUnknownObjectFieldsandchange.TestPrepareChatNormalizesMessagesAndToolHistoryto include an Anthropictype: "image"block and assert it is normalized and tracked (file:internal/openai/chat_prepare_test.go).Testing
go test ./...and all tests succeeded.Codex Task