Back to prompt library
Implement a Pydantic AI Agent for Policy Extraction
Linked challenge: Global Compliance Intelligence Agent
Prompt source
implementation20 lines5 sectionsNo variables1 code block
Using `Pydantic AI`, define an agent that takes raw policy text as input and attempts to extract structured `TaxExemptionPolicy` objects. Integrate `Gemini 2.5 Pro` as the backbone LLM for this extraction. Ensure your agent is configured to produce outputs that strictly adhere to your `TaxExemptionPolicy` model, using Pydantic AI's built-in validation.
```python
from pydantic_ai import PydanticAI
from google.generativeai.client import get_default_retriever
from google.generativeai.generative_models import GenerativeModel
from google.generativeai.types import FunctionDeclaration
# Initialize Gemini 2.5 Pro (ensure proper setup with your API key)
model = GenerativeModel('gemini-1.5-pro-latest') # Or gemini-2.5-pro if available
class PolicyExtractorAgent(PydanticAI):
def extract_policy(self, policy_text: str) -> TaxExemptionPolicy:
"""Extracts structured tax exemption policy details from raw text."""
return self.llm_call(
func=TaxExemptionPolicy, # Target Pydantic model for structured output
prompt=f"Analyze the following policy text and extract a TaxExemptionPolicy object:\n\n{policy_text}"
)
# Instantiate and test the agent
# agent = PolicyExtractorAgent(llm_model=model)
# policy_data = agent.extract_policy(policy_text='...')
# print(policy_data)
```