Return type¶
What does my agent return — text, typed object, or metadata?
Every agent(task) returns an Envelope. What you read off it
depends on what you asked for.
Decision tree¶
Plain string response?
→ result.text() # str, always, regardless of payload type
Pydantic model / structured result?
→ Agent(engine=LLMEngine("…"), output=MyModel)
...
result.payload # MyModel instance
Token count, cost, latency, run id?
→ result.metadata # EnvelopeMetadata
Need to check for errors before reading payload?
→ if result.ok:
read result.payload
else:
read result.error.message
Quick reference¶
| You want | Read |
|---|---|
| Stringified response (works for any payload type) | result.text() |
| Validated typed payload | result.payload (with output=PydanticModel) |
| Token / cost / latency / model / provider | result.metadata |
| Did this run succeed? | result.ok |
| Why did it fail? | result.error.type / result.error.message |
| Original task | result.task |
Notes¶
.text()is always safe. It serialises Pydantic payloads as JSON, returns""forNone, and returns the string verbatim forstrpayloads. Use it whenever you want a string regardless of the payload shape.output=Model+.text()returns JSON, not human-readable text. With structured output, read.payloaddirectly to get the model instance. Calling.text()on a structured envelope is a common confusion — see Envelope pitfalls.metadata.nested_*aren't authoritative for cross-agent rollup. They reflect what flowed through this envelope's lineage, not the entire run. For an authoritative cross-agent cost, querysession.usage_summary().- Always check
.okbefore reading.payloadin production. An error envelope's payload is whatever was last produced (orNone); it'sresult.errorthat tells you something went wrong.