Input & Output

Learn how to pass data to agents and handle their responses.

Agents accept input and generate output in many formats, from simple strings to validated Pydantic models. Start with strings, add structure when you need validation.

Format Types Usage

Use CaseFormat
Prototyping, chat interfacesStrings work fine
Data extraction, classificationStructured output
API responses, pipelinesStructured input and output

String I/O

String in, string out:

1from kern.agent import Agent
2from kern.models.openai import OpenAIResponses
3
4agent = Agent(model=OpenAIResponses(id="gpt-5.2"))
5
6response = agent.run("What's the capital of France?")
7print(response.content) # "The capital of France is Paris."

Structured I/O

Use Pydantic models to validate what goes in and what comes back:

1from pydantic import BaseModel, Field
2from kern.agent import Agent
3from kern.models.openai import OpenAIResponses
4
5class ReviewInput(BaseModel):
6 text: str
7 product_id: str
8
9class SentimentResult(BaseModel):
10 sentiment: str = Field(description="positive, negative, or neutral")
11 confidence: float = Field(ge=0, le=1)
12 summary: str = Field(description="One sentence summary")
13
14agent = Agent(
15 model=OpenAIResponses(id="gpt-5.2"),
16 output_schema=SentimentResult,
17)
18
19response = agent.run(
20 input=ReviewInput(text="Love this product!", product_id="SKU-123")
21)
22
23result: SentimentResult = response.content
24print(result.sentiment) # "positive"
25print(result.confidence) # 0.95

Guides

Advanced I/O Features