Skip to content

Function tool choice causing pydantic validation error #980

Open
@qqaatw

Description

@qqaatw

Please read this first

  • Have you read the docs? Yes
  • Have you searched for related issues? Yes

Describe the bug

A clear and concise description of what the bug is.

This only happens when run with Runner.run_streamed

When a function name is provided to ModelSettings, the converter here will convert it to a dict structure:

tool_choice = Converter.convert_tool_choice(model_settings.tool_choice)

@classmethod
    def convert_tool_choice(
        cls, tool_choice: Literal["auto", "required", "none"] | str | None
    ) -> ChatCompletionToolChoiceOptionParam | NotGiven:
        if tool_choice is None:
            return NOT_GIVEN
        elif tool_choice == "auto":
            return "auto"
        elif tool_choice == "required":
            return "required"
        elif tool_choice == "none":
            return "none"
        else:
            return {
                "type": "function",
                "function": {
                    "name": tool_choice,
                },
            }

However, at the below line, we restrict the input to be one of the three Literals.

tool_choice=cast(Literal["auto", "required", "none"], tool_choice)

Resulting in the below pydantic validation error:

    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 5 validation errors for Response
tool_choice.literal['none','auto','required']
  Input should be 'none', 'auto' or 'required' [type=literal_error, input_value={'type': 'function', 'fun...ognize_named_entities'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error
tool_choice.ToolChoiceTypes.type
  Input should be 'file_search', 'web_search_preview', 'computer_use_preview', 'web_search_preview_2025_03_11', 'image_generation' or 'code_interpreter' [type=literal_error, input_value='function', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error
tool_choice.ToolChoiceFunction.name
  Field required [type=missing, input_value={'type': 'function', 'fun...ognize_named_entities'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing
tool_choice.ToolChoiceMcp.server_label
  Field required [type=missing, input_value={'type': 'function', 'fun...ognize_named_entities'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing
tool_choice.ToolChoiceMcp.type
  Input should be 'mcp' [type=literal_error, input_value='function', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/literal_error

Debug information

  • Agents SDK version: v0.1.0
  • Python version: Python 3.12

Repro steps

Run with Runner.run_streamed and set the model settings to a tool function name instead of the default three Literals.

Expected behavior

Without any validation issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions