Response As Variable

Capture agent responses as variables for downstream use.

Response As Variable.

1"""
2Response As Variable
3=============================
4
5Response As Variable.
6"""
7
8from typing import Iterator # noqa
9from rich.pretty import pprint
10from kern.agent import Agent, RunOutput
11from kern.models.openai import OpenAIResponses
12from kern.tools.yfinance import YFinanceTools
13
14# ---------------------------------------------------------------------------
15# Create Agent
16# ---------------------------------------------------------------------------
17agent = Agent(
18 model=OpenAIResponses(id="gpt-5.2"),
19 tools=[YFinanceTools()],
20 instructions=["Use tables where possible"],
21 markdown=True,
22)
23
24# ---------------------------------------------------------------------------
25# Run Agent
26# ---------------------------------------------------------------------------
27if __name__ == "__main__":
28 run_response: RunOutput = agent.run("What is the stock price of NVDA")
29 pprint(run_response)
30
31 # run_response_strem: Iterator[RunOutputEvent] = agent.run("What is the stock price of NVDA", stream=True)
32 # for response in run_response_strem:
33 # pprint(response)

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