mirror of
https://github.com/hwchase17/langchain.git
synced 2026-07-13 20:47:15 +00:00
## Summary Fixes #34247 When using `Annotated[type, Field(description="...")]` syntax with the `@tool` decorator, field descriptions were being lost during schema generation. The `_get_annotation_description()` function only checked for string annotations but not for Pydantic `FieldInfo` objects. ## Changes - Extended `_get_annotation_description()` to also extract descriptions from `FieldInfo` objects within `Annotated` types - Added import for `pydantic.fields.FieldInfo` - Added unit test to verify `Field(description=...)` is preserved ## Why this approach The fix is minimal and targeted - it extends the existing description extraction logic rather than restructuring the schema generation. This maintains backward compatibility while supporting both annotation styles: ```python # Both now work correctly: topic: Annotated[str, "The research topic"] # existing topic: Annotated[str, Field(description="...")] # now fixed ``` ## Known limitation This fix only handles `pydantic.fields.FieldInfo` (Pydantic v2). The v1 compatibility layer (`pydantic.v1.fields.FieldInfo`) is a different class and will not have descriptions extracted. This is intentional: - Pydantic v1 is deprecated; users should migrate to v2 - The v1 compat layer exists for legacy model migration, not new tool definitions - Duck-typing on `description` attribute could match unintended objects If v1 `Field` support is needed, it can be addressed in a follow-up PR with explicit handling. ## Testing - Added `test_tool_field_description_preserved()` covering required and optional params - Verified existing `test_tool_annotated_descriptions` still passes - Lint and type checks pass --- > [!NOTE] > This PR was developed with AI agent assistance (Factory/Droid). --------- Co-authored-by: Mason Daugherty <github@mdrxy.com>