Fix Anthropic audio-only multipart request validation#62
Conversation
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
There was a problem hiding this comment.
💡 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".
| if hasAudio && !hasSupportedParts { | ||
| throw AIError.invalidInput("AnthropicProvider does not support audio input messages") | ||
| } |
There was a problem hiding this comment.
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 👍 / 👎.
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.buildRequestBodyconverts multipart content by appending supported.textand.imageparts and silently skipping.audio.Changes
buildRequestBody:audioparts with no supported parts (text/image).AIError.invalidInput("AnthropicProvider does not support audio input messages").Audio-only multipart user message is rejectedinvalidInputbehavior and error message content.Why this is safer
Verification
swift test(356 tests passing)swift build(passing)Risk / Compatibility