Input Schema

Pass a dict that matches the input schema.

Input Schema.

1"""
2Input Schema
3=============================
4
5Input Schema.
6"""
7
8from typing import List
9
10from kern.agent import Agent
11from kern.models.openai import OpenAIResponses
12from kern.tools.hackernews import HackerNewsTools
13from pydantic import BaseModel, Field
14
15
16class ResearchTopic(BaseModel):
17 """Structured research topic with specific requirements"""
18
19 topic: str
20 focus_areas: List[str] = Field(description="Specific areas to focus on")
21 target_audience: str = Field(description="Who this research is for")
22 sources_required: int = Field(description="Number of sources needed", default=5)
23
24
25# Define agents
26# ---------------------------------------------------------------------------
27# Create Agent
28# ---------------------------------------------------------------------------
29hackernews_agent = Agent(
30 name="Hackernews Agent",
31 model=OpenAIResponses(id="gpt-5-mini"),
32 tools=[HackerNewsTools()],
33 role="Extract key insights and content from Hackernews posts",
34 input_schema=ResearchTopic,
35)
36
37# ---------------------------------------------------------------------------
38# Run Agent
39# ---------------------------------------------------------------------------
40if __name__ == "__main__":
41 # Pass a dict that matches the input schema
42 hackernews_agent.print_response(
43 input={
44 "topic": "AI",
45 "focus_areas": ["AI", "Machine Learning"],
46 "target_audience": "Developers",
47 "sources_required": "5",
48 }
49 )
50
51 # Pass a pydantic model that matches the input schema
52 hackernews_agent.print_response(
53 input=ResearchTopic(
54 topic="AI",
55 focus_areas=["AI", "Machine Learning"],
56 target_audience="Developers",
57 sources_required=5,
58 )
59 )

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 input_schema.py