Agent with Structured Outputs

Code

1from typing import List
2
3from kern.agent import Agent, RunOutput # noqa
4from kern.models.cerebras import Cerebras
5from pydantic import BaseModel, Field
6from rich.pretty import pprint # noqa
7
8class MovieScript(BaseModel):
9 setting: str = Field(
10 ..., description="Provide a nice setting for a blockbuster movie."
11 )
12 ending: str = Field(
13 ...,
14 description="Ending of the movie. If not available, provide a happy ending.",
15 )
16 genre: str = Field(
17 ...,
18 description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
19 )
20 name: str = Field(..., description="Give a name to this movie")
21 characters: List[str] = Field(..., description="Name of characters for this movie.")
22 storyline: str = Field(
23 ..., description="3 sentence storyline for the movie. Make it exciting!"
24 )
25
26# Agent that uses a JSON schema output with strict_output=True (default)
27json_schema_output_agent = Agent(
28 model=Cerebras(id="llama-3.3-70b"),
29 description="You are a helpful assistant. Summarize the movie script based on the location in a JSON object.",
30 output_schema=MovieScript,
31)
32
33# Agent with strict_output=False (guided mode)
34guided_output_agent = Agent(
35 model=Cerebras(id="llama-3.3-70b", strict_output=False),
36 description="You are a helpful assistant. Summarize the movie script based on the location in a JSON object.",
37 output_schema=MovieScript,
38)
39
40json_schema_output_agent.print_response("New York")
41guided_output_agent.print_response("New York")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export CEREBRAS_API_KEY=xxx

Install dependencies

1uv pip install -U cerebras-cloud-sdk kern-ai

Run Agent

1python cookbook/11_models/cerebras/basic_json_schema.py