microsoft/autogen · error · ValueError

tool_choice 'required' specified but no tools provided

Error message

tool_choice 'required' specified but no tools provided

What it means

When tool_choice='required' is passed to OllamaChatCompletionClient.create(), the client converts the tools list to Ollama's tool format. If that conversion yields an empty list (i.e. no usable tools were provided), it raises ValueError because 'required' tool choice is meaningless without tools. The client refuses to send a contradictory request to Ollama.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py:601

        ollama_messages = [item for sublist in ollama_messages_nested for item in sublist]

        if self.model_info["function_calling"] is False and len(tools) > 0:
            raise ValueError("Model does not support function calling and tools were provided")

        converted_tools: List[OllamaTool] = []

        # Handle tool_choice parameter in a way that is compatible with Ollama API.
        if isinstance(tool_choice, Tool):
            # If tool_choice is a Tool, convert it to OllamaTool.
            converted_tools = convert_tools([tool_choice])
        elif tool_choice == "none":
            # No tool choice, do not pass tools to the API.
            converted_tools = []
        elif tool_choice == "required":
            # Required tool choice, pass tools to the API.
            converted_tools = convert_tools(tools)
            if len(converted_tools) == 0:
                raise ValueError("tool_choice 'required' specified but no tools provided")
        else:
            converted_tools = convert_tools(tools)

        return CreateParams(
            messages=ollama_messages,
            tools=converted_tools,
            format=response_format_value,
            create_args=create_args,
        )

    async def create(
        self,
        messages: Sequence[LLMMessage],
        *,
        tools: Sequence[Tool | ToolSchema] = [],
        tool_choice: Tool | Literal["auto", "required", "none"] = "auto",
        json_output: Optional[bool | type[BaseModel]] = None,
        extra_create_args: Mapping[str, Any] = {},

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass a non-empty tools list whenever tool_choice='required' is set
  2. Guard the call: only set tool_choice='required' when len(tools) > 0, otherwise use 'auto' or omit it
  3. Fix upstream logic that produces an empty tools list when it should not be empty

Example fix

# before
await client.create(messages, tools=filtered_tools, tool_choice='required')  # crashes when filtered_tools == []

# after
tool_choice = 'required' if tools else 'auto'
await client.create(messages, tools=tools, tool_choice=tool_choice)
Defensive patterns

Strategy: type-guard

Validate before calling

if tool_choice == 'required' and len(tools) == 0:
    tool_choice = 'auto'  # or raise your own config error
result = await client.create(messages, tools=tools, tool_choice=tool_choice)

Type guard

from typing import Any

def valid_tool_choice(tool_choice: Any, tools) -> bool:
    if tool_choice == 'required':
        return len(tools) > 0
    return tool_choice in ('auto', 'none') or hasattr(tool_choice, 'schema')

Try / catch

try:
    result = await client.create(messages, tools=tools, tool_choice='required')
except ValueError as e:
    if "'required' specified but no tools" in str(e):
        result = await client.create(messages, tools=tools, tool_choice='auto')
    else:
        raise

Prevention

When it happens

Trigger: Calling create(messages, tools=[], tool_choice='required') or passing tool_choice='required' with a tools sequence that converts to zero Ollama tools (e.g. empty list, or entries convert_tools drops).

Common situations: Agent code that always sets tool_choice='required' but dynamically builds the tools list, which can be empty on some turns; refactoring that accidentally drops the tools argument while keeping tool_choice; copy-paste from an example that used a fixed tool set.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/0efac4136c868a13. Report an issue: GitHub.