Skip to content

Commit 566e5a7

Browse files
committed
Refactor token structure in streaming_query to use dictionaries for tool execution events and improve handling of turn completion. Update tests to reflect changes in response format and ensure proper assertions.
1 parent b5afee1 commit 566e5a7

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

src/app/endpoints/streaming_query.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ def _handle_turn_complete_event(chunk: Any, chunk_id: int) -> Iterator[str]:
213213
"event": "turn_complete",
214214
"data": {
215215
"id": chunk_id,
216-
"token": chunk.event.payload.turn.output_message.content,
216+
"token": interleaved_content_as_str(
217+
chunk.event.payload.turn.output_message.content
218+
),
217219
},
218220
}
219221
)
@@ -335,7 +337,10 @@ def _handle_tool_execution_event(
335337
"data": {
336338
"id": chunk_id,
337339
"role": chunk.event.payload.step_type,
338-
"token": f"Tool:{t.tool_name} arguments:{t.arguments}",
340+
"token": {
341+
"tool_name": t.tool_name,
342+
"arguments": t.arguments,
343+
},
339344
},
340345
}
341346
)
@@ -349,7 +354,10 @@ def _handle_tool_execution_event(
349354
"data": {
350355
"id": chunk_id,
351356
"role": chunk.event.payload.step_type,
352-
"token": f"Fetched {len(inserted_context)} bytes from memory",
357+
"token": {
358+
"tool_name": r.tool_name,
359+
"response": f"Fetched {len(inserted_context)} bytes from memory",
360+
},
353361
},
354362
}
355363
)
@@ -380,7 +388,10 @@ def _handle_tool_execution_event(
380388
"data": {
381389
"id": chunk_id,
382390
"role": chunk.event.payload.step_type,
383-
"token": f"Tool:{r.tool_name} summary:{summary}",
391+
"token": {
392+
"tool_name": r.tool_name,
393+
"summary": summary,
394+
},
384395
},
385396
}
386397
)
@@ -392,7 +403,10 @@ def _handle_tool_execution_event(
392403
"data": {
393404
"id": chunk_id,
394405
"role": chunk.event.payload.step_type,
395-
"token": f"Tool:{r.tool_name} response:{r.content}",
406+
"token": {
407+
"tool_name": r.tool_name,
408+
"response": interleaved_content_as_str(r.content),
409+
},
396410
},
397411
}
398412
)
@@ -446,16 +460,20 @@ async def streaming_query_endpoint_handler(
446460
async def response_generator(turn_response: Any) -> AsyncIterator[str]:
447461
"""Generate SSE formatted streaming response."""
448462
chunk_id = 0
449-
complete_response = ""
463+
complete_response = "No response from the model"
450464

451465
# Send start event
452466
yield stream_start_event(conversation_id)
453467

454468
async for chunk in turn_response:
455469
for event in stream_build_event(chunk, chunk_id, metadata_map):
456-
complete_response += json.loads(event.replace("data: ", ""))[
457-
"data"
458-
]["token"]
470+
if (
471+
json.loads(event.replace("data: ", ""))["event"]
472+
== "turn_complete"
473+
):
474+
complete_response = json.loads(event.replace("data: ", ""))[
475+
"data"
476+
]["token"]
459477
chunk_id += 1
460478
yield event
461479

tests/unit/app/endpoints/test_streaming_query.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,21 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
181181
payload=AgentTurnResponseStepProgressPayload(
182182
event_type="step_progress",
183183
step_type="inference",
184-
delta=TextDelta(text="LLM answer", type="text"),
184+
delta=TextDelta(text="LLM ", type="text"),
185185
step_id="s1",
186186
)
187187
)
188188
),
189+
AgentTurnResponseStreamChunk(
190+
event=TurnResponseEvent(
191+
payload=AgentTurnResponseStepProgressPayload(
192+
event_type="step_progress",
193+
step_type="inference",
194+
delta=TextDelta(text="answer", type="text"),
195+
step_id="s2",
196+
)
197+
)
198+
),
189199
AgentTurnResponseStreamChunk(
190200
event=TurnResponseEvent(
191201
payload=AgentTurnResponseStepCompletePayload(
@@ -194,7 +204,7 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
194204
step_type="tool_execution",
195205
step_details=ToolExecutionStep(
196206
turn_id="t1",
197-
step_id="s2",
207+
step_id="s3",
198208
step_type="tool_execution",
199209
tool_responses=[
200210
ToolResponse(
@@ -215,6 +225,27 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
215225
)
216226
)
217227
),
228+
AgentTurnResponseStreamChunk(
229+
event=TurnResponseEvent(
230+
payload=AgentTurnResponseTurnCompletePayload(
231+
event_type="turn_complete",
232+
turn=Turn(
233+
turn_id="t1",
234+
input_messages=[],
235+
output_message=CompletionMessage(
236+
role="assistant",
237+
content=[TextContentItem(text="LLM answer", type="text")],
238+
stop_reason="end_of_turn",
239+
),
240+
session_id="test_session_id",
241+
started_at=datetime.now(),
242+
steps=[],
243+
completed_at=datetime.now(),
244+
output_attachments=[],
245+
),
246+
)
247+
)
248+
),
218249
]
219250

220251
query = "What is OpenStack?"
@@ -263,8 +294,8 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
263294
assert "LLM answer" in full_content
264295

265296
# Assert referenced documents
266-
assert len(streaming_content) == 5
267-
d = json.loads(streaming_content[4][5:])
297+
assert len(streaming_content) == 7
298+
d = json.loads(streaming_content[6][5:])
268299
referenced_documents = d["data"]["referenced_documents"]
269300
assert len(referenced_documents) == 2
270301
assert referenced_documents[1]["doc_title"] == "Doc2"
@@ -277,8 +308,7 @@ async def _test_streaming_query_endpoint_handler(mocker, store_transcript=False)
277308
query_is_valid=True,
278309
query=query,
279310
query_request=query_request,
280-
response="LLM answerTool:knowledge_search arguments:{}Tool:knowledge_search "
281-
"summary:knowledge_search tool found 2 chunks:",
311+
response="LLM answer",
282312
attachments=[],
283313
rag_chunks=[],
284314
truncated=False,
@@ -940,12 +970,12 @@ def test_stream_build_event_step_complete():
940970
assert result is not None
941971
assert "data: " in result
942972
assert '"event": "tool_call"' in result
943-
assert '"token": "Tool:knowledge_search arguments:' in result
973+
assert '"token": {"tool_name": "knowledge_search", "arguments": {}}' in result
944974

945975
result = next(itr)
946976
assert (
947-
'"token": "Tool:knowledge_search summary:knowledge_search tool found 2 chunks:"'
948-
in result
977+
'"token": {"tool_name": "knowledge_search", '
978+
'"summary": "knowledge_search tool found 2 chunks:"}' in result
949979
)
950980
assert '"role": "tool_execution"' in result
951981
assert '"id": 0' in result

0 commit comments

Comments
 (0)