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ΒΆ

PromptStyle

Different styles for generating prompts from Pydantic models.

PydanticPromptConfig

Configuration for Pydantic model to prompt conversion.

FunctionsΒΆ

analyze_pydantic_field(field_info, field_name)

Analyze a Pydantic field to extract information for prompt generation.

analyze_type_annotation(annotation)

Analyze a type annotation to extract useful information.

create_example_from_model(model_class)

Create an example output from a Pydantic model.

create_generation_and_parsing_prompts(model_class[, ...])

Create both generation and parsing prompts for structured output pattern.

create_parsing_prompt(model_class[, content_field])

Create a prompt for parsing content into a Pydantic model.

create_pydantic_prompt(model_class, config[, ...])

Create a prompt template from a Pydantic model.

generate_field_description(field_analysis, style)

Generate a description for a field based on analysis and style.

quick_pydantic_prompt(model_class[, style, use_json])

Quick way to create a basic prompt from a Pydantic model.

Module ContentsΒΆ

class agents.common.utils.pydantic_prompt_utils.PromptStyleΒΆ

Bases: str, enum.Enum

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.

Parameters:
  • field_info (Any) – Pydantic FieldInfo object

  • field_name (str) – Name of the field

Returns:

Dictionary containing field analysis

Return type:

dict[str, Any]

agents.common.utils.pydantic_prompt_utils.analyze_type_annotation(annotation)ΒΆ

Analyze a type annotation to extract useful information.

Parameters:

annotation (type) – Type annotation to analyze

Returns:

Dictionary containing type analysis

Return type:

dict[str, Any]

agents.common.utils.pydantic_prompt_utils.create_example_from_model(model_class)ΒΆ

Create an example output from a Pydantic model.

Parameters:

model_class (type[pydantic.BaseModel]) – Pydantic model class

Returns:

Example string showing the expected format

Return type:

str

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.

Parameters:
  • model_class (type[pydantic.BaseModel]) – Pydantic model class to parse into

  • content_field (str) – Name of the field containing the content to parse

Returns:

ChatPromptTemplate for parsing content

Return type:

langchain_core.prompts.ChatPromptTemplate

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:

str

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