This page requires Python familiarity. It covers accessing conversation history from Python functions.
The conv.history attribute contains a chronological list of UserInput and AgentResponse objects representing the conversation so far.
Filter events with isinstance() rather than event.role — new event types may be added that don’t have a role attribute.
Represents a user turn in the conversation.
for event in conv.history:
if isinstance(event, UserInput):
print(f"User said: {event.text}")
Properties
| Property | Type | Description |
|---|
text | str | The user’s input text. Can be an empty string when the user is silent or no speech is detected. |
role | str | Always "user" |
Methods
| Method | Returns | Description |
|---|
to_dict() | dict | Returns {"type": "user", "text": "..."} |
to_string() | str | Returns "User: <text>" |
AgentResponse
Represents an agent turn in the conversation.
for event in conv.history:
if isinstance(event, AgentResponse):
print(f"Agent said: {event.text}")
Properties
| Property | Type | Description |
|---|
text | str | The agent’s response text |
role | str | Always "agent" |
Methods
| Method | Returns | Description |
|---|
to_dict() | dict | Returns {"type": "agent", "text": "..."} |
to_string() | str | Returns "Agent: <text>" |
Example: format history for logging
def log_conversation(conv: Conversation):
for event in conv.history:
conv.log.info(event.to_string())
Example: get last user message
def get_last_user_input(conv: Conversation) -> str:
for event in reversed(conv.history):
if isinstance(event, UserInput):
return event.text
return ""
Notes
- Empty
text values: UserInput.text can be an empty string (e.g., when a user is silent or no speech is detected). Guard against this in your functions if downstream logic depends on non-empty input:
last_input = get_last_user_input(conv)
if last_input.strip():
# Process the input
pass
- Metric events: Metric data is not part of
conv.history. Custom metrics are written separately via conv.write_metric.
See also
conv object – full list of conversation methods and attributes.