Structured Output With Tool Use

Code

1from kern.agent import Agent
2from kern.models.mistral import MistralChat
3from kern.tools.hackernews import HackerNewsTools
4from pydantic import BaseModel
5
6class Person(BaseModel):
7 name: str
8 description: str
9
10model = MistralChat(
11 id="mistral-medium-latest",
12 temperature=0.0,
13)
14
15researcher = Agent(
16 name="Researcher",
17 model=model,
18 role="You find people with a specific role at a provided company.",
19 instructions=[
20 "- Search the web for the person described"
21 "- Find out if they have public contact details"
22 "- Return the information in a structured format"
23 ],
24 tools=[HackerNewsTools()],
25 output_schema=Person,
26 add_datetime_to_context=True,
27)
28
29researcher.print_response("Find information about Elon Musk")

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 MISTRAL_API_KEY=xxx

Install dependencies

1uv pip install -U mistralai kern-ai

Run Agent

1python cookbook/11_models/mistral/structured_output_with_tool_use.py