Parser Model

Get the response in a variable.

Parser Model.

1"""
2Parser Model
3=============================
4
5Parser Model.
6"""
7
8import random
9from typing import List
10
11from kern.agent import Agent, RunOutput # noqa
12from kern.models.openai import OpenAIResponses
13from pydantic import BaseModel, Field
14from rich.pretty import pprint # noqa
15
16
17class 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 )
63
64
65# ---------------------------------------------------------------------------
66# Create Agent
67# ---------------------------------------------------------------------------
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)
74
75
76# Get the response in a variable
77national_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]
89
90# ---------------------------------------------------------------------------
91# Run Agent
92# ---------------------------------------------------------------------------
93if __name__ == "__main__":
94 # Get the response in a variable
95 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 repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/02_input_output
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python parser_model.py