agents.common.utils.pydantic_prompt_utilsΒΆ
Utilities for converting Pydantic models to prompt templates.
This module provides utilities to create prompt templates from Pydantic models, supporting the structured output pattern where prompts focus on generation and parsers handle the structured parsing separately.
Key features: - Generate prompts that guide LLMs to create content parseable by Pydantic models - Support for different prompt styles (descriptive, example-based, schema-based) - Field-specific guidance and constraints - Optional examples and formatting hints
ClassesΒΆ
Different styles for generating prompts from Pydantic models. |
|
Configuration for Pydantic model to prompt conversion. |
FunctionsΒΆ
|
Analyze a Pydantic field to extract information for prompt generation. |
|
Analyze a type annotation to extract useful information. |
|
Create an example output from a Pydantic model. |
|
Create both generation and parsing prompts for structured output pattern. |
|
Create a prompt for parsing content into a Pydantic model. |
|
Create a prompt template from a Pydantic model. |
|
Generate a description for a field based on analysis and style. |
|
Quick way to create a basic prompt from a Pydantic model. |
Module ContentsΒΆ
- class agents.common.utils.pydantic_prompt_utils.PromptStyleΒΆ
-
Different styles for generating prompts from Pydantic models.
Initialize self. See help(type(self)) for accurate signature.
- class agents.common.utils.pydantic_prompt_utils.PydanticPromptConfig(/, **data)ΒΆ
Bases:
pydantic.BaseModel
Configuration for Pydantic model to prompt conversion.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- agents.common.utils.pydantic_prompt_utils.analyze_pydantic_field(field_info, field_name)ΒΆ
Analyze a Pydantic field to extract information for prompt generation.
- agents.common.utils.pydantic_prompt_utils.analyze_type_annotation(annotation)ΒΆ
Analyze a type annotation to extract useful information.
- agents.common.utils.pydantic_prompt_utils.create_example_from_model(model_class)ΒΆ
Create an example output from a Pydantic model.
- agents.common.utils.pydantic_prompt_utils.create_generation_and_parsing_prompts(model_class, generation_instruction='Generate comprehensive content about the topic:', config=None)ΒΆ
Create both generation and parsing prompts for structured output pattern.
This follows the best practice of separating content generation from structured parsing, allowing the LLM to focus on creating good content first, then extracting structure from that content.
- Parameters:
model_class (type[pydantic.BaseModel]) β Pydantic model class
generation_instruction (str) β Instruction for content generation
config (PydanticPromptConfig | None) β Configuration for prompt generation
- Returns:
Tuple of (generation_prompt, parsing_prompt)
- Return type:
tuple[langchain_core.prompts.ChatPromptTemplate, langchain_core.prompts.ChatPromptTemplate]
- agents.common.utils.pydantic_prompt_utils.create_parsing_prompt(model_class, content_field='content')ΒΆ
Create a prompt for parsing content into a Pydantic model.
This creates a separate parsing prompt that can be used after generation to extract structured information from unstructured content.
- agents.common.utils.pydantic_prompt_utils.create_pydantic_prompt(model_class, config, base_instruction='Generate content with the following structure:')ΒΆ
Create a prompt template from a Pydantic model.
- Parameters:
model_class (type[pydantic.BaseModel]) β Pydantic model class to create prompt for
config (PydanticPromptConfig) β Configuration for prompt generation
base_instruction (str) β Base instruction for the prompt
- Returns:
ChatPromptTemplate that guides LLM to generate parseable content
- Return type:
langchain_core.prompts.ChatPromptTemplate
- agents.common.utils.pydantic_prompt_utils.generate_field_description(field_analysis, style)ΒΆ
Generate a description for a field based on analysis and style.
- Parameters:
field_analysis (dict[str, Any]) β Field analysis from analyze_pydantic_field
style (PromptStyle) β Prompt style to use
- Returns:
Description string for the field
- Return type:
- agents.common.utils.pydantic_prompt_utils.quick_pydantic_prompt(model_class, style=PromptStyle.DESCRIPTIVE, use_json=False)ΒΆ
Quick way to create a basic prompt from a Pydantic model.
- Parameters:
model_class (type[pydantic.BaseModel]) β Pydantic model class
style (PromptStyle) β Prompt style to use
use_json (bool) β Whether to request JSON format
- Returns:
ChatPromptTemplate for the model
- Return type:
langchain_core.prompts.ChatPromptTemplate