Skip to content

Conversation

@moonbox3
Copy link
Contributor

Motivation and Context

When using Literal type annotations for tool parameters in @ai_function decorated methods, Pydantic throws:

PydanticUserError: search_flows_input is not fully defined; you should define Data, then call search_flows_input.model_rebuild().

The _parse_annotation function incorrectly processed Literal types. For Literal["Data", "Security"], the args are ("Data", "Security"). The code checked if args[1] is a string (which "Security" is), then mistakenly created Annotated[ForwardRef('Data'), Field(description='Security')], treating literal values as type references.

The fix: added early return in _parse_annotation when get_origin(annotation) is Literal to preserve Literal types unchanged.

Testing:

Added 9 tests covering:

  • Literal string/int/bool types in standalone functions and class methods
  • Literal combined with Annotated and Field
  • Unit tests for _parse_annotation function

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 self-assigned this Dec 16, 2025
Copilot AI review requested due to automatic review settings December 16, 2025 04:10
@github-actions github-actions bot changed the title Fix Pydantic error when using Literal type for tool params Python: Fix Pydantic error when using Literal type for tool params Dec 16, 2025
@moonbox3 moonbox3 added the agents Issues related to single agents label Dec 16, 2025
@moonbox3 moonbox3 moved this to In Review in Agent Framework Dec 16, 2025
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _tools.py6876590%224, 270, 321, 323, 486, 518–519, 620, 622, 642, 660, 674, 686, 691, 693, 700, 733, 787–789, 827, 850, 852–861, 870–876, 912, 922, 1106, 1479–1483, 1583, 1652, 1753, 1759, 1802–1803, 1938, 1979–1980, 2008–2010, 2045–2046, 2111–2112, 2119–2120
TOTAL16604257384% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2406 130 💤 0 ❌ 0 🔥 57.295s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a Pydantic error that occurred when using Literal type annotations for tool parameters in @ai_function decorated methods. The issue was that _parse_annotation incorrectly processed Literal types, treating their literal values as type references and creating invalid Annotated types.

Key changes:

  • Added early return for Literal types in _parse_annotation to preserve them unchanged
  • Added comprehensive test coverage with 9 new tests covering various Literal use cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/_tools.py Fixed _parse_annotation to return Literal types unchanged, preventing Pydantic errors
python/packages/core/tests/core/test_tools.py Added 9 tests covering Literal types with strings/ints/bools in functions and class methods, plus unit tests for _parse_annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Issues related to single agents python

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

Python: Pydantic error when attempting to use Literal type for tool parameter

4 participants