Skip to main content

@llm_function

@llm_function turns a Python function signature into a complete LLM call. One input, one typed output. No history, no tools (or tools with auto-loop), no state management.

Basic Usage

How the System Prompt Is Built

Your docstring is the starting point, but the framework augments it:
  1. Your docstring → task policy, quality bar, constraints
  2. Parameter types → automatically described for the model
  3. Return type schema → output format instructions (XML-based structured extraction)
  4. Tool best practices → prepended if tools are mounted
You write what the model should DO. The framework handles what the model should OUTPUT.

Return Types

The framework generates an XML schema from the Pydantic model and instructs the model to produce matching output. Parsing is automatic.

Plain str (for free-form text)

Lists and nested types

Primitive types

Template Parameters

Inject runtime values into the docstring:

Multimodal Image Parameters

@llm_function keeps the normal Python-function shape for image input: declare explicit image parameters in the function signature.
Use ImgUrl for web URLs or data: URLs. Use ImgPath for local files; the framework encodes the image as a data URL before sending it to the model. Multiple images should be expressed as explicit parameters or typed lists such as list[ImgUrl] / list[ImgPath].

With Tools

@llm_function can use tools via a ReAct loop:
The function still returns a single typed result, but internally the model may call tools multiple times before producing the final answer. max_tool_calls limits how many tool calls the model can make. None means unlimited.

Event Stream Mode

@llm_function returns an LLMFunction callable instance. Normal calls use await fn(...); use fn.stream(...) for ReactOutput:
For simple usage, collect the final response:

AbortSignal

Cancel a running call:

Parameters Reference

Special call-time parameters (prefixed with _):
  • _template_params: Dict[str, Any] — template values for docstring formatting
  • _abort_signal: AbortSignal — cancellation signal
API Reference: Decorators