Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/app/endpoints/conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,13 @@ async def get_conversation_endpoint_handler(
try:
client = AsyncLlamaStackClientHolder().get_client()

session_data = (await client.agents.session.list(agent_id=agent_id)).data[0]
agent_sessions = (await client.agents.session.list(agent_id=agent_id)).data
session_id = str(agent_sessions[0].get("session_id"))

session_response = await client.agents.session.retrieve(
agent_id=agent_id, session_id=session_id
)
session_data = session_response.model_dump()
Comment on lines +137 to +143
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Good fix for the llama-stack upgrade issue, but add empty sessions check.

The two-step approach correctly addresses the issue where the upgraded llama-stack no longer returns conversation turns in the list response. However, there's a potential IndexError if no sessions exist.

Add a check for empty sessions:

 agent_sessions = (await client.agents.session.list(agent_id=agent_id)).data
+if not agent_sessions:
+    logger.warning("No sessions found for conversation %s", conversation_id)
+    return ConversationResponse(
+        conversation_id=conversation_id,
+        chat_history=[],
+    )
 session_id = str(agent_sessions[0].get("session_id"))
🤖 Prompt for AI Agents
In src/app/endpoints/conversations.py around lines 137 to 143, add a check to
verify that agent_sessions is not empty before accessing the first element to
avoid an IndexError. If agent_sessions is empty, handle this case appropriately,
such as returning early or raising a clear exception. This ensures the code
safely handles cases where no sessions exist.


logger.info("Successfully retrieved conversation %s", conversation_id)

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/app/endpoints/test_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ async def test_successful_conversation_retrieval(
mock_client.agents.session.list.return_value = mocker.Mock(
data=[mock_session_data]
)

# Mock session.retrieve to return an object with model_dump() method
mock_session_retrieve_result = mocker.Mock()
mock_session_retrieve_result.model_dump.return_value = mock_session_data
mock_client.agents.session.retrieve.return_value = mock_session_retrieve_result

mock_client_holder = mocker.patch(
"app.endpoints.conversations.AsyncLlamaStackClientHolder"
)
Expand Down