How to ask a agent question and get back the answe...
# ask-ai
m
How to ask a agent question and get back the answer in string vaiable in AgentSession like call_summary = session.generate_reply("give the call summary")
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring the
session.generate_reply()
method! This method is primarily designed for voice agents to generate and play audio responses, and it returns a
SpeechHandle
object instead of a string. But don't worry—you can still get the generated text by checking the conversation history or by listening to conversation events. Here are two friendly ways to do that: 1. Accessing Conversation History:
Copy code
# After the agent has responded
call_summary = session.history.messages[-1].text_content
2. Using the
conversation_item_added
Event:
Copy code
call_summary = None

@session.on("conversation_item_added")
async def on_conversation_item_added(item):
    global call_summary
    if item.role == "assistant":
        call_summary = item.text_content

# Trigger the reply
generate_reply("give the call summary")
These approaches let you capture the agent's response as a string variable, so you can use it however you need. If you have any questions or want to explore more, feel free to ask—I'm here to help! Sources: examples/voice_agents/realtime_load_chat_history.py | Agent speech and audio | LiveKit Docs | agents/src/voice/agent_session.ts