fix: accept system role in messages[] and handle built-in Anthropic tools#79
Open
HtetOoWaiYan wants to merge 1 commit into
Open
fix: accept system role in messages[] and handle built-in Anthropic tools#79HtetOoWaiYan wants to merge 1 commit into
HtetOoWaiYan wants to merge 1 commit into
Conversation
Two related 422 validation fixes: 1. ClaudeMessage.role now accepts "system" in addition to "user" and "assistant". Newer Claude models (e.g. claude-opus-4-8) send interleaved system-role messages inside the messages[] array rather than exclusively via the top-level system field. Pydantic was rejecting these requests before they reached the converter. 2. ClaudeTool.input_schema is now Optional (default None) and two new optional fields (type, max_uses) are added to accommodate built-in Anthropic tools such as web_search_20250305 and computer_use_20251201 that carry no input_schema. The converter now skips these tools (logging a debug message) rather than crashing with a 422. Fixes fuergaosi233#26 (422 error with streaming API / newer models) Fixes fuergaosi233#67 (Web Search tool causes 422 due to missing input_schema)
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.
Summary
Two fixes for 422 validation errors introduced by newer Claude models and Anthropic built-in tools.
Fix 1: Accept
systemrole inmessages[](closes #26)ClaudeMessage.rolewasLiteral["user", "assistant"], which caused Pydantic to reject requests with a 422 before they reached the converter.Newer Claude models (e.g.
claude-opus-4-8) send interleavedsystem-role messages inside themessages[]array, not only via the top-levelsystemfield. Example request that was being rejected:{ "messages": [ {"role": "user", "content": "..."}, {"role": "system", "content": "..."}, {"role": "user", "content": "..."} ] }Changes:
ClaudeMessage.rolenow accepts"system"as a valid valuerequest_converter.pyhandlesrole: "system"messages in the loop, converting them to OpenAI system messages instead of silently dropping themFix 2: Handle built-in Anthropic tools (closes #67)
Built-in Anthropic tools like
web_search_20250305andcomputer_use_20251201are sent without aninput_schema. SinceClaudeTool.input_schemawas a required field, this caused a 422 immediately.Changes:
ClaudeTool.input_schemais nowOptional(defaults toNone)typeandmax_usesadded toClaudeToolto accept the full built-in tool shapeinput_schema(logging a debug message) rather than attempting to forward them to the OpenAI endpoint, which wouldn't understand them anywayTest plan
system-role message insidemessages[]— should return 200 instead of 422tools: [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}]— should return 200 instead of 422 (tool will be skipped/not forwarded)