Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Microsoft Autogen Function Tool Definition

From Leeroopedia
Knowledge Sources
Domains Tool Use, Function Calling, LLM Agents, Schema Generation
Last Updated 2026-02-11 00:00 GMT

Overview

Function tool definition is the process of wrapping a native programming function with metadata and a JSON schema so that a large language model can discover, understand, and invoke it through structured tool calling.

Description

Large language models that support tool calling (also called function calling) do not execute code directly. Instead, they produce structured JSON outputs that specify which function to call and what arguments to pass. For this mechanism to work, the framework must present each available function to the LLM as a tool schema -- a JSON Schema document that describes the function's name, purpose, and the types and constraints of its parameters.

Function tool definition bridges the gap between native code and LLM-compatible tool schemas. The process involves:

  • Schema derivation: Automatically inspecting a function's type annotations and signature to generate a JSON Schema for its parameters. This eliminates the need for developers to manually author schemas, reducing boilerplate and preventing schema drift when the function signature changes.
  • Description attachment: Associating a natural-language description with the function. The LLM uses this description to decide when the tool is appropriate for a given task.
  • Name resolution: Determining the tool's name, either from the function's own name or from an explicit override. The name is what the LLM references when requesting a tool call.
  • Return type tracking: Recording the function's return type so the framework can properly serialize and present results back to the LLM.
  • Cancellation support: Detecting whether the function accepts a cancellation token parameter, enabling cooperative cancellation of long-running tool executions.

The resulting tool object is a self-contained unit that can be registered with an agent, listed in tool schemas sent to the LLM, and invoked when the LLM returns a matching function call.

Usage

Use function tool definition when:

  • You have existing Python functions (sync or async) that you want to make available to an LLM agent without writing manual JSON Schema definitions.
  • You need type-safe tool invocation where argument validation is handled automatically via the generated Pydantic model.
  • You want to rapidly prototype tool-augmented agents by wrapping simple helper functions (web search, file I/O, calculations, API calls) as tools.
  • You need to expose domain-specific logic to an LLM while maintaining a clean separation between the function's implementation and the LLM interaction layer.

Theoretical Basis

Function tool definition relies on the Adapter pattern from software design, translating between the LLM's expected tool schema interface and the developer's native function interface. The core algorithm is:

FUNCTION define_tool(func, description, name_override, strict_mode):
    1. Extract the typed signature from func (inspect parameters and annotations)
    2. Resolve the tool name:
       - If name_override is provided, use it
       - If func is a partial, use the wrapped function's __name__
       - Otherwise, use func.__name__
    3. Generate a Pydantic BaseModel from the signature:
       - Each parameter becomes a model field
       - Type annotations map to JSON Schema types
       - Default values become field defaults
    4. Detect cancellation support:
       - If "cancellation_token" is in parameters, mark as cancellation-aware
    5. Extract the return type annotation
    6. If strict_mode is enabled:
       - Ensure the schema conforms to strict JSON Schema rules
       - All fields required, no additional properties
    7. Store (args_model, return_type, name, description) as the tool definition
    8. Return a Tool object that can:
       - Produce a ToolSchema (name + description + JSON Schema)
       - Execute by deserializing JSON args into the model and calling func

The schema generation step is critical because it transforms Python's runtime type system into the JSON Schema type system that LLMs understand. This includes mapping Python types such as str, int, float, bool, List, Dict, and Optional to their JSON Schema equivalents (string, integer, number, boolean, array, object, and nullable types).

The strict mode option enforces additional JSON Schema constraints required by some LLM providers (such as OpenAI's strict function calling mode), where every property must be explicitly listed as required and additionalProperties must be false.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment