rohitg00/ai-engineering-from-scratch · error · ValueError

tc.mode

Error message

tc.mode

What it means

Sentinel fallback guard at the end of tool_choice_openai: tc.mode matched none of 'auto', 'none', 'required', or 'force', so the raw mode value is re-raised as the error text. The input at fault is ToolChoice.mode — typically a typo or a provider-specific spelling that was never normalized.

Solutions

  1. Set tc.mode to one of 'auto', 'none', 'required', or 'force'
  2. Validate ToolChoice.mode at construction time instead of at translation time
  3. Map provider-specific mode spellings before calling tool_choice_openai
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/13-tools-and-protocols/02-function-calling-deep-dive/code/main.py:113 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/8d8effe34b9ff2ab. Report an issue: GitHub.

Appendix: source

Thrown at phases/13-tools-and-protocols/02-function-calling-deep-dive/code/main.py:113

            {
                "name": tool.name,
                "description": tool.description,
                "parameters": _gemini_schema(tool.input_schema),
            }
        ]
    }


def tool_choice_openai(tc: ToolChoice) -> Any:
    if tc.mode == "auto":
        return "auto"
    if tc.mode == "none":
        return "none"
    if tc.mode == "required":
        return "required"
    if tc.mode == "force":
        return {"type": "function", "function": {"name": tc.tool_name}}
    raise ValueError(tc.mode)


def tool_choice_anthropic(tc: ToolChoice) -> dict:
    if tc.mode == "auto":
        return {"type": "auto"}
    if tc.mode == "none":
        return {"type": "none"}
    if tc.mode == "required":
        return {"type": "any"}
    if tc.mode == "force":
        return {"type": "tool", "name": tc.tool_name}
    raise ValueError(tc.mode)


def tool_choice_gemini(tc: ToolChoice) -> dict:
    mode_map = {"auto": "AUTO", "none": "NONE", "required": "ANY"}
    if tc.mode in mode_map:
        return {"function_calling_config": {"mode": mode_map[tc.mode]}}

View on GitHub (pinned to 39ea8a1c6d)