[BUG] Structured output schema drops union branches: Union[A, B] and int | str lose their type, Optional[Union[A, B]] keeps only B
Human Overview
- If a Pydantic field can be one of several types, like Cat | Dog or int | str, the schema sent to the model has no type at all for that field.
- Optional[Cat | Dog] keeps only Dog.
- Why it matters: the model can’t know what shape to return, so structured output breaks for these models.
Checks
- [x] I have updated to the lastest minor and patch version of Strands
- [x] I have checked the documentation and this is not expected behavior
- [x] I have searched ./issues and there are no duplicates of my issue
SDK Language
Python
Strands Version
main at 1060ccee1 (1.64.1.dev91)
Language Runtime Version
Python 3.12.10, pydantic 2.14.3
Operating System
Windows 11
Installation Method
git clone
Steps to Reproduce
import json
from typing import Literal, Optional, Union
from pydantic import BaseModel
from strands.tools.structured_output import convert_pydantic_to_tool_spec
class Cat(BaseModel):
kind: Literal["cat"]
meows: int
class Dog(BaseModel):
kind: Literal["dog"]
barks: int
class Pet(BaseModel):
pet: Union[Cat, Dog]
maybe_pet: Optional[Union[Cat, Dog]] = None
id: int | str
print(json.dumps(convert_pydantic_to_tool_spec(Pet)["inputSchema"]["json"]["properties"], indent=2))
Expected Behavior
Each union keeps all of its branches (an anyOf over Cat and Dog, and over integer and string), as in Pet.model_json_schema().
Actual Behavior
"pet": {"title": "Pet"}
"maybe_pet": {"title": "Dog", "type": ["object", "null"], "properties": {... Dog only ...}}
"id": {"title": "Id"}
petandidlose their type entirely, so the model is told nothing about these fields.maybe_petkeeps only the last non-null branch, so the model is told the field must be aDog; aCatis never offered.
This is the spec that structured_output_model sends through the structured output tool, and that Bedrock, Anthropic and LiteLLM use in structured_output().
Possible Solution
In _process_property, the anyOf branch only handles exactly one null option plus one other option. For any other anyOf, non_null_type is overwritten by each option (the last one wins), and when there is no null option the code falls through to the copy loop, which skips the anyOf key. Keeping anyOf (with each $ref option resolved) whenever there is more than one non-null option would fix both cases.
Additional Context
The Union[Cat, Dog] and Optional[Union[Cat, Dog]] shapes were also mentioned in a review comment on #3591, which was closed in favour of #4610. #4610 fixed nullable enums but not this, and I could not find an issue tracking it.
Happy to send a fix if that helps.
Drafted with Claude Code (AI); the repro above was run locally against main.
I'd like to work on this. The report shows convert_pydantic_to_tool_spec dropping the type for Union[Cat, Dog] and int | str, and keeping only Dog for Optional[Union[Cat, Dog]]. I plan to make the schema cleanup in strands.tools.structured_output keep anyOf branches (resolving each branch's refs and keeping the null branch for Optional) so the output matches Pet.model_json_schema(). I'll add tests using the Pet model from the issue that check all three fields keep every branch. Per CONTRIBUTING, could you assign this to me before I start? I'll complete the Human Overview myself and say in the PR how AI was used.