Skip to content

Fix Anthropic audio-only multipart request validation#62

Merged
christopherkarani merged 3 commits into
mainfrom
codex/check-frameworks-for-issues-20260517
May 18, 2026
Merged

Fix Anthropic audio-only multipart request validation#62
christopherkarani merged 3 commits into
mainfrom
codex/check-frameworks-for-issues-20260517

Conversation

@christopherkarani
Copy link
Copy Markdown
Owner

Summary

This PR fixes a correctness gap in Anthropic request construction where unsupported audio-only multipart messages were serialized into empty multipart payloads, causing downstream Anthropic API validation failures with unclear diagnostics.

Root Cause

  • AnthropicProvider.buildRequestBody converts multipart content by appending supported .text and .image parts and silently skipping .audio.
  • For audio-only user/assistant multipart messages, this produced an empty multipart array.
  • The request then failed only at API time, obscuring the real issue (unsupported input modality).

Changes

  • Added preflight validation in buildRequestBody:
    • Scans user/assistant multipart messages.
    • Detects audio parts with no supported parts (text/image).
    • Fails early with AIError.invalidInput("AnthropicProvider does not support audio input messages").
  • Added a regression test:
    • Audio-only multipart user message is rejected
    • Verifies the exact invalidInput behavior and error message content.

Why this is safer

  • Converts an opaque remote validation failure into a deterministic local input validation error.
  • Prevents invalid outbound payloads from leaving the client.
  • Improves debugging and correctness for multimodal integrations.

Verification

  • swift test (356 tests passing)
  • swift build (passing)

Risk / Compatibility

  • No behavior change for normal text/image Anthropic flows.
  • Mixed content with at least one supported part still works; unsupported audio parts remain ignored in that mixed case.

Root cause:\n- Anthropic request building silently dropped audio parts.\n- Audio-only multipart messages became empty multipart payloads and failed later at the API boundary with unclear diagnostics.\n\nFix:\n- Add preflight validation in buildRequestBody that detects audio-only multipart user/assistant messages.\n- Fail fast with AIError.invalidInput("AnthropicProvider does not support audio input messages").\n- Preserve existing behavior for mixed payloads that include supported text/image parts.\n\nVerification:\n- swift test\n- swift build
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8d558932c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Comment on lines +101 to +103
if hasAudio && !hasSupportedParts {
throw AIError.invalidInput("AnthropicProvider does not support audio input messages")
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip audio-only guard for assistant tool-call messages

This early invalidInput throw rejects any assistant .parts message that contains only audio, but later in buildRequestBody assistant tool calls are built from message.metadata.toolCalls (not from content parts), so an assistant message with tool calls and audio-only content would have produced a valid tool_use payload before this change. In that scenario, this guard turns a previously valid tool-call continuation into a hard failure even though no audio part would be sent to Anthropic.

Useful? React with 👍 / 👎.

@christopherkarani christopherkarani merged commit ebaf07f into main May 18, 2026
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant