Fix Union type handling in Pydantic output generation

- Fixed generate_model_description to correctly handle Union types
- Union types without None are now properly formatted as Union[type1, type2]
- Union types with None are correctly wrapped in Optional[Union[...]]
- Added support for Python 3.10+ pipe syntax (int | str | None)
- Added comprehensive tests for Union type support
- Updated existing test expectations to match corrected behavior

This fixes issue #3735 where Union types were incorrectly wrapped in Optional
even when None was not part of the Union.

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-10-19 21:33:03 +00:00
parent 42f2b4d551
commit f76c55ffa7
3 changed files with 220 additions and 5 deletions

View File

@@ -596,5 +596,5 @@ def test_generate_model_description_union_field():
field: int | str | None
description = generate_model_description(UnionModel)
expected_description = '{\n "field": int | str | None\n}'
expected_description = '{\n "field": Optional[Union[int, str]]\n}'
assert description == expected_description