An AI agent that answers like an API, at zero cost (Google ADK + Pydantic)

A language model answers differently every time. That is fine for a person, not for your program. In the previous episode the agent moved to a local model; now it answers in a fixed format, like an API, still with no per-call cost. This is the fourth video of the series ADK, from beginner to advanced (in Portuguese), and the code is in the repository .
The agent
import os
from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm
from pydantic import BaseModel, Field
class CapitalOutput(BaseModel):
capital: str = Field(description="The capital of the country.")
root_agent = LlmAgent(
model=LiteLlm(model=os.getenv("LLM_MODEL", "lm_studio/google/gemma-4-e4b")),
name="capital_agent",
description="Returns the capital of a country as JSON.",
instruction='You are a Capital Information Agent. Given a country, respond ONLY with a JSON object containing the capital. Format: {"capital": "capital_name"}',
output_schema=CapitalOutput,
output_key="found_capital", # resultado fica em state["found_capital"]
)What is new compared to the previous episodes is the CapitalOutput class and two LlmAgent parameters. The code comment is in Portuguese: the result is stored in state["found_capital"].
output_schema: the shape of the answer
CapitalOutput inherits from Pydantic
’s BaseModel. It describes the answer: a string field named capital, with a description. With the class in output_schema, the agent is expected to answer in that shape, and the response is validated against the class.
The instruction also asks for JSON and shows the format. That helps mostly with small local models.
output_key: where the result goes
output_key="found_capital" writes the answer to the session state, in state["found_capital"]. Another step of your application, or another agent, can read it without parsing free text.
Running it
- In LM Studio, load the model and start the local server (same
.envas episode 3). - In the project:
uv sync
uv run adk webPick capital_agent and ask for the capital of Brazil. The answer comes back as JSON:
{"capital": "Brasília"}The adk web state tab shows found_capital with the same value.
Caveats
- A correct shape does not mean a correct answer: the schema validates structure, not facts.
- Small models may return invalid JSON. Handle the validation error in your application.
- Combining
output_schemawith tools depends on the model and the ADK version. Check the LlmAgent docs first.
Next steps
With structured answers in state, the next step is more than one agent working together: multi-agent. Where would you use an agent that answers in JSON? Tell me in the video comments.
- Playlist: ADK, from beginner to advanced
- Previous episode: Google ADK with a local model
- Code for this episode: youtube-projects/ADK-Iniciante ao Avancado/04-structured-outputs



kubectl get events