Back to Prompt Library
implementation
Implement a Pydantic AI Agent for Policy Extraction
Inspect the original prompt language first, then copy or adapt it once you know how it fits your workflow.
Linked challenge: Global Compliance Intelligence Agent
Format
Code-aware
Lines
20
Sections
5
Linked challenge
Global Compliance Intelligence Agent
Prompt source
Original prompt text with formatting preserved for inspection.
20 lines
5 sections
No variables
1 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)
```Adaptation plan
Keep the source stable, then change the prompt in a predictable order so the next run is easier to evaluate.
Keep stable
Hold the task contract and output shape stable so generated implementations remain comparable.
Tune next
Update libraries, interfaces, and environment assumptions to match the stack you actually run.
Verify after
Test failure handling, edge cases, and any code paths that depend on hidden context or secrets.