Skip to content

Commit 05cdd46

Browse files
fix collecting final response with openai responses streaming (#20037)
1 parent 9c6c510 commit 05cdd46

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/responses.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ def stream_complete(
450450

451451
return stream_complete_fn(prompt, **kwargs)
452452

453-
def _parse_response_output(self, output: List[ResponseOutputItem]) -> ChatResponse:
453+
@staticmethod
454+
def _parse_response_output(output: List[ResponseOutputItem]) -> ChatResponse:
454455
message = ChatMessage(role=MessageRole.ASSISTANT, blocks=[])
455456
additional_kwargs = {"built_in_tool_calls": []}
456457
tool_calls = []
@@ -526,7 +527,7 @@ def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
526527
if self.track_previous_responses:
527528
self._previous_response_id = response.id
528529

529-
chat_response = self._parse_response_output(response.output)
530+
chat_response = OpenAIResponses._parse_response_output(response.output)
530531
chat_response.raw = response
531532
chat_response.additional_kwargs["usage"] = response.usage
532533
if hasattr(response.usage.output_tokens_details, "reasoning_tokens"):
@@ -590,7 +591,6 @@ def process_response_event(
590591
elif isinstance(event, ResponseTextDeltaEvent):
591592
# Text content is being added
592593
delta = event.delta
593-
blocks.append(TextBlock(text=delta))
594594
elif isinstance(event, ResponseImageGenCallPartialImageEvent):
595595
# Partial image
596596
if event.partial_image_b64:
@@ -653,6 +653,8 @@ def process_response_event(
653653
# Response is complete
654654
if hasattr(event, "response") and hasattr(event.response, "usage"):
655655
additional_kwargs["usage"] = event.response.usage
656+
resp = OpenAIResponses._parse_response_output(event.response.output)
657+
blocks = resp.message.blocks
656658

657659
return (
658660
blocks,
@@ -782,7 +784,7 @@ async def _achat(
782784
if self.track_previous_responses:
783785
self._previous_response_id = response.id
784786

785-
chat_response = self._parse_response_output(response.output)
787+
chat_response = OpenAIResponses._parse_response_output(response.output)
786788
chat_response.raw = response
787789
chat_response.additional_kwargs["usage"] = response.usage
788790

llama-index-integrations/llms/llama-index-llms-openai/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dev = [
2727

2828
[project]
2929
name = "llama-index-llms-openai"
30-
version = "0.6.2"
30+
version = "0.6.3"
3131
description = "llama-index llms openai integration"
3232
authors = [{name = "llama-index"}]
3333
requires-python = ">=3.9,<4.0"

llama-index-integrations/llms/llama-index-llms-openai/tests/test_openai_responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_process_response_event():
153153
)
154154

155155
updated_blocks, updated_tool_calls, _, _, _, _, delta = result
156-
assert updated_blocks == [TextBlock(text="Hello")]
156+
assert updated_blocks == []
157157
assert delta == "Hello"
158158
assert updated_tool_calls == []
159159

0 commit comments

Comments
 (0)