Parser Model
Get the response in a variable.
Parser Model.
1"""2Parser Model3=============================45Parser Model.6"""78import random9from typing import List1011from kern.agent import Agent, RunOutput # noqa12from kern.models.openai import OpenAIResponses13from pydantic import BaseModel, Field14from rich.pretty import pprint # noqa151617class NationalParkAdventure(BaseModel):18 park_name: str = Field(..., description="Name of the national park")19 best_season: str = Field(20 ...,21 description="Optimal time of year to visit this park (e.g., 'Late spring to early fall')",22 )23 signature_attractions: List[str] = Field(24 ...,25 description="Must-see landmarks, viewpoints, or natural features in the park",26 )27 recommended_trails: List[str] = Field(28 ...,29 description="Top hiking trails with difficulty levels (e.g., 'Angel's Landing - Strenuous')",30 )31 wildlife_encounters: List[str] = Field(32 ..., description="Animals visitors are likely to spot, with viewing tips"33 )34 photography_spots: List[str] = Field(35 ...,36 description="Best locations for capturing stunning photos, including sunrise/sunset spots",37 )38 camping_options: List[str] = Field(39 ..., description="Available camping areas, from primitive to RV-friendly sites"40 )41 safety_warnings: List[str] = Field(42 ..., description="Important safety considerations specific to this park"43 )44 hidden_gems: List[str] = Field(45 ..., description="Lesser-known spots or experiences that most visitors miss"46 )47 difficulty_rating: int = Field(48 ...,49 ge=1,50 le=5,51 description="Overall park difficulty for average visitor (1=easy, 5=very challenging)",52 )53 estimated_days: int = Field(54 ...,55 ge=1,56 le=14,57 description="Recommended number of days to properly explore the park",58 )59 special_permits_needed: List[str] = Field(60 default=[],61 description="Any special permits or reservations required for certain activities",62 )636465# ---------------------------------------------------------------------------66# Create Agent67# ---------------------------------------------------------------------------68agent = Agent(69 model=OpenAIResponses(id="gpt-5.2"),70 description="You help people plan amazing national park adventures and provide detailed park guides.",71 output_schema=NationalParkAdventure,72 parser_model=OpenAIResponses(id="gpt-5.2"),73)747576# Get the response in a variable77national_parks = [78 "Yellowstone National Park",79 "Yosemite National Park",80 "Grand Canyon National Park",81 "Zion National Park",82 "Grand Teton National Park",83 "Rocky Mountain National Park",84 "Acadia National Park",85 "Mount Rainier National Park",86 "Great Smoky Mountains National Park",87 "Rocky National Park",88]8990# ---------------------------------------------------------------------------91# Run Agent92# ---------------------------------------------------------------------------93if __name__ == "__main__":94 # Get the response in a variable95 run: RunOutput = agent.run(96 national_parks[random.randint(0, len(national_parks) - 1)]97 )98 pprint(run.content)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/02_input_output45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python parser_model.py