fix: normalize structured_output to handle wrapper keys and stringified JSON#532
Open
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Open
fix: normalize structured_output to handle wrapper keys and stringified JSON#532wingding12 wants to merge 1 commit intoanthropics:mainfrom
wingding12 wants to merge 1 commit intoanthropics:mainfrom
Conversation
…ed JSON Fix two common issues with model-generated structured output: 1. Wrapper keys (anthropics#502): Model sometimes wraps data in {"output": {...}}, {"response": {...}}, {"json": {...}}, etc. This fix unwraps single-key dicts with common wrapper keys to return just the inner data. 2. Stringified JSON (anthropics#510): Model sometimes serializes arrays/objects as JSON strings instead of native arrays. This fix recursively parses such strings back to native Python types. The normalization is applied when parsing ResultMessage, so users get clean structured_output without needing to handle these edge cases. Fixes anthropics#502 Fixes anthropics#510
|
@wingding12 why so closed? |
|
Why is this closed without being merged? we are still facing this issue |
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
Fix two common issues with model-generated structured output that cause schema validation failures and silent
Noneresults:Issue #502: Wrapper Keys
Model sometimes wraps data in
{"output": {...}},{"response": {...}},{"json": {...}}, etc. This causes schema validation to fail because the wrapper key is an "additional property".Before:
{"output": {"actions": [...]}}→ schema validation failsAfter:
{"actions": [...]}→ works correctlyIssue #510: Stringified JSON Arrays
Model sometimes serializes arrays as JSON strings like
"[{\"field\": ...}]"instead of native arrays. This causes repeated validation failures with errors like.items: should be array.Before:
{"items": "[{\"field\": \"value\"}]"}→ validation failsAfter:
{"items": [{"field": "value"}]}→ works correctlyChanges
src/claude_agent_sdk/_internal/message_parser.py: Added_normalize_structured_output()and_parse_stringified_json()functions that:output,response,json,data,result)ResultMessageparsingtests/test_structured_output_normalization.py: Added 25 tests covering:Test Plan
Fixes #502
Fixes #510