Raise on unhandled content types in sampling handler dispatch chains#3857
Merged
Raise on unhandled content types in sampling handler dispatch chains#3857
Conversation
90a2235 to
f156cc5
Compare
The Anthropic and OpenAI sampling handlers have isinstance chains that dispatch on MCP content types but silently drop unhandled variants like EmbeddedResource and ResourceLink. This adds explicit else-raise guards to match the Gemini handler's behavior and the single-content dispatch paths that already raise. Raising is the right choice over warn-and-skip: a partial conversion produces a plausible-but-wrong LLM response (the model confidently answers based on incomplete input), which is worse than a clear error that tells the user exactly what isn't supported. 🤖 Generated with Claude Code Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
f156cc5 to
9d6b045
Compare
Tests the new ValueError raises for unsupported content types (e.g. EmbeddedResource) in the Anthropic and OpenAI message conversion loops. Uses model_construct to bypass Pydantic's union validation since the raise is a defensive guard for future SDK content types. 🤖 Generated with Claude Code Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
jlowin
approved these changes
Apr 12, 2026
Member
jlowin
left a comment
There was a problem hiding this comment.
Exhaustiveness is the right posture for message conversion — silent-drop was the bug. Matches the Gemini handler and single-content paths.
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.
The Anthropic and OpenAI sampling handlers have isinstance chains that dispatch on MCP content types (
TextContent,ImageContent,AudioContent,ToolUseContent,ToolResultContent) but silently drop unhandled variants likeEmbeddedResourceandResourceLink.This adds explicit
else: raise ValueErrorto the two list-content dispatch loops that were missing catch-all branches, matching the behavior of:Why raise instead of warn-and-skip: These handlers convert MCP messages to vendor API formats for LLM calls. A partial conversion produces a plausible-but-wrong response — the LLM confidently answers based on incomplete input, and the user has no reason to doubt it. A clear error is less surprising than a subtly wrong answer.
🤖 Generated with Claude Code