Skip to content

Fix Anthropic image content normalization#8

Merged
Futureppo merged 1 commit into
mainfrom
513rgx-codex/v1messages
Jul 13, 2026
Merged

Fix Anthropic image content normalization#8
Futureppo merged 1 commit into
mainfrom
513rgx-codex/v1messages

Conversation

@Futureppo

Copy link
Copy Markdown
Owner

Motivation

  • Fix handling of Anthropic-style type: "image" content in /v1/messages so it no longer triggers unsupported-type errors during OpenAI chat preparation.
  • Ensure Anthropic image blocks (URL and base64 sources) are normalized into OpenAI-compatible image_url parts and client-provided metadata is stripped/recorded as a change.

Description

  • Added a new case "image" branch in prepareMessageContent to accept Anthropic image blocks and map them to image_url parts (file: internal/openai/chat_prepare.go).
  • Implemented prepareAnthropicImage to validate source objects, support url and base64 sources (constructing data:<media_type>;base64,<data>), and record the rewrite with recordUnknownObjectFields and change.
  • Updated the chat preparation regression test TestPrepareChatNormalizesMessagesAndToolHistory to include an Anthropic type: "image" block and assert it is normalized and tracked (file: internal/openai/chat_prepare_test.go).

Testing

  • Ran the full test suite with go test ./... and all tests succeeded.

Codex Task

@Futureppo Futureppo merged commit 355d801 into main Jul 13, 2026
4 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +501 to +506
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant