Week 13 • Day 1Week 13
Day 1:Week 13, Day 1: Function & Tool Calling Internals — Pydantic-to-JSON-Schema & Safe Tool Dispatch
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Week 13, Day 1: Function & Tool Calling Internals — Pydantic-to-JSON-Schema & Safe Tool Dispatch
1. Demystifying LLM "Tool Calling": The LLM Never Executes Code!
Beginners often think an LLM directly runs Python functions or database queries.
First-Principles Reality: An LLM is strictly a Text/Tokens In -> Text/Tokens Out mathematical function!
Here is the exact 4-step protocol of Tool Calling (Function Calling):
code
┌──────────────┐ 1. Prompt + JSON Schema of Tools ┌──────────────────┐
│ │ ────────────────────────────────────► │ │
│ Your Python │ 2. Structured ToolCall Intent JSON │ LLM API │
│ Orchestrator│ ◄──────────────────────────────────── │ (Gemini/GPT/ │
│ (Host App) │ {"name": "get_pod_status", │ Claude/Llama) │
│ │ "args": {"namespace": "prod"}} │ │
│ │ │ │
│ 3. Validates│ │ │
│ withPydantic│ 4. Appends ToolResult message & │ │
│ & Executes │ calls LLM again for Final Answer │ │
│ Python Func │ ────────────────────────────────────► │ │
└──────────────┘ └──────────────────┘
2. The 3 Engineering Rules of Production Tool Registries
- Strict Pydantic Input Validation: Never pass raw LLM-generated
dictarguments directly into**kwargs! Validate through apydantic.BaseModelschema first so invalid types, missing fields, or out-of-bounds numbers are caught cleanly. - Never Let a Tool Crash the Orchestrator Loop: If a tool raises an exception (or Pydantic validation fails), catch it and return a structured
ToolExecutionResult(is_error=True, content=...)back to the LLM so the LLM can read the error and self-correct on the next turn! - Rich Docstrings & Field Descriptions: The LLM reads your
descriptionfields in the JSON Schema to decide when and how to call each tool.
In Simple English: Giving an LLM Safe Hands (Tool Calling)
An LLM by itself can only output text — it cannot check live server logs or look up your bank balance.
Function / Tool Calling works like handing a chef a printed restaurant order slip (JSON Schema):
- The LLM does NOT run Python directly!
- Instead, it outputs structured JSON:
{"tool": "check_pod_logs", "args": {"pod": "payments-7f9"}}. - Your Backend Code validates that JSON against strict Pydantic rules, checks permissions, runs the real Python function safely, and hands the text output back to the AI!