Skip to content

Python: [Bug]: Function calling allowed_tools does not work with mode: required for OpenAI clients #7023

Description

@cecheta

Description

When using allowed_tools with function calling, mode should be allowed to be set to either auto or required, as is the case in the core package:

raise ContentError("tool_choice 'allowed_tools' is only valid when mode is 'auto' or 'required'")

However, the OpenAI client only supports allowed_tools with mode: auto, and drops the allowed tools if mode: required is set.

Code Sample

In this example, you would expect the function subtract_numbers to be called, but you get add_numbers {"number1":3,"number2":5}.

import asyncio

from agent_framework import Agent, FunctionTool
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
from pydantic import BaseModel


class NumberInput(BaseModel):
    number1: int
    number2: int


async def main() -> None:
    client = FoundryChatClient(
        credential=AzureCliCredential(),
    )

    agent = Agent(
        client=client,
        name="CalculatorAgent",
        instructions="You are a helpful calculator agent. Use the tools to answer questions.",
        tools=[
            FunctionTool(
                name="add_numbers",
                description="Add two numbers together.",
                input_model=NumberInput,
            ),
            FunctionTool(
                name="subtract_numbers",
                description="Subtract the second number from the first number.",
                input_model=NumberInput,
            ),
        ],
    )

    outputs = await agent.run(
        "Add 3 and 5.",
        options={
            "tool_choice": {
                "mode": "required",
                "allowed_tools": ["subtract_numbers"],
            }
        },
    )

    print(outputs.messages[0].contents[0].name, outputs.messages[0].contents[0].arguments)  # add_numbers {"number1":3,"number2":5}


if __name__ == "__main__":
    asyncio.run(main())

Error Messages / Stack Traces

Package Versions

agent-framework-core: 1.10.0, agent-framework-openai: 1.10.0

Python Version

3.13.13

Additional Context

No response

Metadata

Metadata

Assignees

Labels

agentsUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions