Skip to main content

Overview

Templates are predefined structures used to create consistent outputs. In the context of AI applications, prompt templates provide structured guidance for language models to generate targeted responses. They include placeholders that can be filled with specific information at runtime. The Framework implements this functionality through the PromptTemplate class, which uses Mustache-style syntax (via the chevron library) for variable substitution. The implementation adds type safety and validation using Pydantic or Zod schemas. At its core, the PromptTemplate class:
  • Validates input data against a Pydantic model schema
  • Handles template variable substitution
  • Supports dynamic content generation through callable functions
  • Provides default values for optional fields
  • Enables template customization through forking
Prompt Templates are fundamental building blocks in the framework and are extensively used in agent implementations.
Supported in Python and TypeScript.

Basic usage

Simple template

Create templates with basic variable substitution and type validation.
This example creates a simple template that formats a user message with a label and input text. The Pydantic model or Zod schema ensures type safety for the template variables.

Template functions

Add dynamic content to templates using custom functions.
This example demonstrates how to add custom functions to templates:
  • The format_meta function returns the date and author in a readable string
  • Functions can be called directly from the template using Mustache-style syntax

Working with objects

Handle complex nested data structures in templates with proper type validation.
This example shows how to work with nested objects in templates. The Mustache syntax allows for iterating through the responses array and accessing properties of each object.

Working with arrays

Process collections of data within templates for dynamic list generation.
This example demonstrates how to iterate over arrays in templates using Mustache’s section syntax. Source: python/examples/templates/arrays.py

Template forking

The fork() method allows you to create new templates based on existing ones, with customizations. Template forking is useful for:
  • Creating variations of templates while maintaining core functionality
  • Adding new fields or functionality to existing templates
  • Specializing generic templates for specific use cases
Python
This example shows how to create a new template based on an existing one. Source: python/examples/templates/forking.py

Default values

Provide default values for template variables that can be overridden at runtime.

Using templates with agents

The framework’s agents use specialized templates to structure their behavior. You can customize these templates to alter how agents operate:
Python
This example demonstrates how to create a system prompt for an agent with tool definitions, which enables the agent to use external tools like weather data retrieval. Source: python/examples/templates/system_prompt.py

Examples

Python

Explore reference template implementations in Python

TypeScript

Explore reference template implementations in TypeScript