Agent Harness Tutorial · End to end · Zoom level 2
Reading the answer
The model's reply arrives as loose parts: some text, some requests to call tools. One function, the "typed boundary", turns those parts into our own strict types. After this point no code ever handles raw model output.
Tool requests. Each one becomes ToolCall(id, name, arguments). The arguments must be a JSON object; anything else is "broken". Whether the arguments fit the tool is checked later, at the tool gate, so the model can be told what was wrong and try again.
Words only. This is a candidate answer, not the answer. If Rohan asked two things and the reply covers only one, the model is sent back once with "Also raised and not yet dealt with: ...". Then the post-model check reads it.
Broken. Malformed output is not retried, because asking again at temperature 0 tends to give the same broken output. The turn ends with "I am having trouble answering right now."
Code view: input, process, output, the call stack, the classes
How it is done: the real code
Cut from the live repositories each time the site is built, so it always matches what runs.
agent_harness/llm/pydantic_ai.py:133The typed boundarydef from_response(response: pa.ModelResponse) -> ModelResponse:
"""The typed boundary (AHC-0001), on Pydantic AI's parsed response. The same
two outcomes as `_from_wire`: a response, or `ModelMalformed` for tool
arguments that are not a JSON object and for a paid-for answer with neither
text nor a call (F-031)."""
calls: list[ToolCall] = []
for part in response.parts:
if not isinstance(part, pa.ToolCallPart):
continue
try:
arguments = part.args_as_dict(raise_if_invalid=True)
except (ValueError, AssertionError) as exc:
raise ModelMalformed(
f"tool call {part.tool_name!r} has unreadable arguments: {exc}",
raw=str(part.args),
) from exc
calls.append(ToolCall(id=part.tool_call_id, name=part.tool_name, arguments=arguments))
text = "".join(p.content for p in response.parts if isinstance(p, pa.TextPart))
usage = response.usage
if not text.strip() and not calls:
raise ModelMalformed(
"the model returned neither text nor a tool call after "
f"{usage.output_tokens} output tokens"
+ (
" — the output budget was spent before an answer began"
if response.finish_reason == "length"
else ""
),
raw=text,
)
# The provider's own word for why it stopped, where it gave one, so the loop
# reads `length` and `tool_calls` as it does from the other client.
details = response.provider_details or {}
return ModelResponse(Tech choices (what we chose, Azure and open-source options): L6 · I/O contracts