Sub topic 9
Reading the response and message types
This chapter assumes you can already create a session and send a message, as shown in the previous chapters. Here you will look at what comes back and how to handle it in code.
The shape of a synchronous reply
A successful POST /sessions/{sessionId}/messages call returns JSON with a messages array. The array can hold more than one message, so reading only the first element is a shortcut and not a safe default.
{
"messages": [
{
"type": "Inform",
"id": "a1b2c3d4",
"message": "Your order shipped on Monday.",
"feedbackId": "f-123",
"planId": "p-456",
"isContentSafe": true,
"result": [],
"citedReferences": []
}
]
}The fields you will use most are:
| Field | Meaning |
|---|---|
| `type` | The kind of message. Decide how to handle it from this value. |
| `message` | The text to show the user. |
| `id` | Identifies this message. Useful in logs. |
| `feedbackId` | Identifies the reply when you send feedback about it. |
| `isContentSafe` | Reports whether the content passed safety checks. Check it before you display the text. |
Treat the exact field set as something to confirm in the Agent API reference for your org's release. Your code should ignore fields it does not recognize.
Message types
The type value tells you what the agent is doing.
Informis a normal answer or statement.Inquireis a question back to the user, such as a request for an order number. Your client should collect an answer and send it as the next message.Failuremeans the agent could not complete the request.Escalatemeans the agent is handing the conversation to a human or another channel.SessionEndedmeans the agent closed the session.
Other types exist, such as progress indicators and stream chunk types. These mostly appear when you stream, which a later chapter covers. Write your handler so that an unknown type is logged and skipped, not treated as an error.
A parser that handles multiple messages
This function turns a reply into a small result your application can act on. It runs as shown, and the sample call at the bottom uses the JSON from above.
from dataclasses import dataclass
@dataclass
class ParsedReply:
text: str
needs_input: bool = False
escalated: bool = False
session_ended: bool = False
failed: bool = False
def parse_reply(body: dict) -> ParsedReply:
result = ParsedReply(text="")
parts = []
for msg in body.get("messages", []):
kind = msg.get("type")
text = msg.get("message") or ""
if kind == "Inform":
parts.append(text)
elif kind == "Inquire":
parts.append(text)
result.needs_input = True
elif kind == "Failure":
result.failed = True
elif kind == "Escalate":
result.escalated = True
elif kind == "SessionEnded":
result.session_ended = True
else:
print(f"Ignoring message type: {kind}")
result.text = "\n".join(p for p in parts if p)
return result
sample = {
"messages": [
{"type": "Inform", "id": "a1b2c3d4", "message": "Your order shipped on Monday."}
]
}
print(parse_reply(sample))Joining all text-bearing messages keeps the user from losing part of a multi-message reply. The flags let the calling code decide what happens next. For example, it can prompt the user again when needs_input is true, or skip DELETE when session_ended is true.
Common mistakes
- Reading
messages[0]["message"]and crashing when the array is empty or the first entry is not text. - Showing the text without checking
isContentSafe. - Treating
Inquirelike a final answer, so the conversation stalls while the agent waits. - Raising an exception on an unknown
type, which breaks your integration when a new type is added.
Exercise
The agent replies with one message of type Inquire and the text "Which order number?". What should your client do, and why is the Inform-only handler from the sample wrong here?
Show answer
The client should show the question, collect the user's answer, and send it as the next message in the same session. An Inform-only handler would drop the question, so the user would never see it and the agent would keep waiting.