Tool Output Formatting: Markdown vs Structured Dictionaries for LLM Consumption
Choose what a tool actually returns: structured dicts with status and summary beat Markdown for chaining and callbacks, while prose belongs in the agent's final answer.
Published on • November 28, 2026
AI Assistant

Every tool return is a message the model must read, reason over, and possibly hand to another tool — so the format is not a cosmetic choice. A Markdown table reads well and parses poorly; a dict parses perfectly and reads like noise. The wrong pick shows up as failed chains (“the second tool got a string where it expected an ID”), bloated context, and models that re-run a query because they misread the first result.
This tutorial covers tool return payloads — what lands in the FunctionResponse. If you need the agent’s final answer in a fixed shape, that is the output_schema / output_key problem, covered in output serialization from Markdown to JSON; the two boundaries are decided separately and often with different formats.
Markdown vs dict at a glance
| Markdown string | Structured dict | |
|---|---|---|
| Readability for the model | High at small sizes | High once keys are stable |
| Extracting one field | Relies on parsing prose | Direct key access |
| Feeding a follow-up tool | Fragile — the model transcribes | Exact values survive |
| Error signalling | A sentence that may be misread | status + error_message the model branches on |
| Token cost per row | Padding (` | , **`) per cell |
| Testing | Golden strings | Assertions on keys |
| Human display | Ready to paste | Needs conversion |
The rule of thumb: structure in the tool, prose in the answer. The model is excellent at turning {'state': 'refunded'} into a natural sentence, and poor at reliably turning a wall of Markdown back into a value another tool accepts.
The default pipeline, and what ADK does with your return
A function tool’s return value becomes the body of a FunctionResponse the model reads on the next step. In Python the preferred return type is a dict; anything else — a str, a list — gets wrapped into {"result": <value>} before it reaches the model. Two consequences:
def get_inventory(sku: str) -> dict:
"""Check stock for a SKU.
Args:
sku (str): Stock keeping unit, e.g. 'SKU-221'.
Returns:
dict: {'status': 'success'|'error', 'available': int, 'summary': str}
or {'status': 'error', 'error_message': str}.
"""
item = inventory.lookup(sku)
if item is None:
return {"status": "error", "error_message": f"No item with SKU {sku}."}
return {
"status": "success",
"available": item.available,
"warehouse": item.warehouse,
"summary": f"{sku} has {item.available} units in {item.warehouse}.",
}
- Return a string and your docstring’s promised shape silently becomes
{"result": "..."}— fine for a one-off, untestable for a contract. - Return a dict without a
statuskey and the model has no reliable branch for failure; the documented best practice is to include one (success/error/pending).
Shape the dict like an API response
A payload the model can use has four zones: outcome, data, provenance, and a quotable line.
return {
"status": "success", # 1. outcome — the branch point
"items": [{"id": o.id, "state": o.state} for o in orders], # 2. data
"row_count": len(orders), # 3. provenance — count, truncation, ids
"truncated": len(orders) >= 50,
"summary": f"Found {len(orders)} orders, states: "
f"{sorted({o.state for o in orders})}.", # 4. quotable
}
The summary is the bridge: the model’s final response is written from it, so the user-facing prose costs one sentence rather than a second pass over the raw structure. When the payload is large, follow pagination and batch patterns — return a page, a cursor, and a has_more flag instead of everything.
When Markdown genuinely wins
Markdown is the right return type in narrow cases:
- The tool’s job is presentation — rendering a comparison table the user will see, or returning a formatted snippet that goes into a chat reply verbatim.
- The payload is prose — a document excerpt, a log tail, a diff — where restructuring it would destroy the information.
- The consumer is a human in the dev UI, not a chain of tools.
Even then, keep a status beside it: {"status": "success", "report_md": "..."} gives you the branch and the formatting.
Reshaping after the fact with after_tool_callback
If a third-party tool returns the wrong shape, you do not have to fork it. after_tool_callback runs just after a tool completes successfully, and returning a new dict replaces the original tool_response; returning None leaves it alone. Three traps documented by ADK worth memorising:
Noneis the only passthrough. The framework compares againstNone, so returning{}does not mean “no change” — it replaces the response with an empty dict.- The wrap happens after the callback. A tool annotated
-> strhands your callback astr, not a dict — check the type before calling.get(). - Use it for redaction and reduction, not for inventing data the tool never produced.
def compact_result(callback_context, args, tool_response):
if isinstance(tool_response, dict) and "rows" in tool_response:
rows = tool_response["rows"][:5]
tool_response = {**tool_response, "rows": rows, "truncated": True,
"summary": f"First 5 of {len(tool_response['rows'])} rows."}
return tool_response # None would pass the original through untouched
Size is part of format
A perfectly shaped 60 KB dict is still a mistake. Practically: keep tool results under a few KB, prefer summaries over enumerations, and remember that tool outputs re-enter the context on every subsequent turn of the invocation — which is why bounded payloads matter even more than elegant ones (token budgeting). For the receiving side of the same decision — how the model’s own final output is serialised — see the output serialization post.
Takeaways
- Return a dict: ADK prefers it, non-dict values get wrapped as
{"result": ...}, and dicts survive chaining and testing. - Always include
status; branch-worthy failures carry a human-readableerror_message, not a numeric code. - Put one quotable
summaryline in every success payload — it becomes the model’s answer without a second parsing pass. - Reserve Markdown for payloads that are prose or display-ready; structure in the tool, prose in the answer.
after_tool_callbackcan replace a bad third-party shape — returningNoneis the only passthrough, andstrarrives unwrapped.- Bound the size: a tool result is re-read every later turn, so pagination beats padding.