Skip to content

Commit e428e9c

Browse files
committed
[Python] accumulate code_interpreter delta chunks into single content item (fixes #5793)
1 parent 70875f7 commit e428e9c

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

‎python/packages/openai/agent_framework_openai/_chat_client.py‎

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ def _inner_get_response(
613613
if stream:
614614
function_call_ids: dict[int, tuple[str, str]] = {}
615615
seen_reasoning_delta_item_ids: set[str] = set()
616+
code_interpreter_accumulator: dict[str, str] = {}
616617
validated_options: dict[str, Any] | None = None
617618
# Captured once request options are validated/prepared so the streaming finalizer can
618619
# still parse the aggregated response into structured output after the stream completes.
@@ -648,6 +649,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
648649
options=validated_options,
649650
function_call_ids=function_call_ids,
650651
seen_reasoning_delta_item_ids=seen_reasoning_delta_item_ids,
652+
code_interpreter_accumulator=code_interpreter_accumulator,
651653
)
652654
if served_model is not None:
653655
update.model = served_model
@@ -676,6 +678,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
676678
options=validated_options,
677679
function_call_ids=function_call_ids,
678680
seen_reasoning_delta_item_ids=seen_reasoning_delta_item_ids,
681+
code_interpreter_accumulator=code_interpreter_accumulator,
679682
)
680683
else:
681684
raw_create_response = await client.responses.with_raw_response.create(
@@ -690,6 +693,7 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]:
690693
options=validated_options,
691694
function_call_ids=function_call_ids,
692695
seen_reasoning_delta_item_ids=seen_reasoning_delta_item_ids,
696+
code_interpreter_accumulator=code_interpreter_accumulator,
693697
)
694698
if served_model is not None:
695699
update.model = served_model
@@ -2343,6 +2347,7 @@ def _parse_chunk_from_openai(
23432347
options: dict[str, Any],
23442348
function_call_ids: dict[int, tuple[str, str]],
23452349
seen_reasoning_delta_item_ids: set[str] | None = None,
2350+
code_interpreter_accumulator: dict[str, str] | None = None,
23462351
) -> ChatResponseUpdate:
23472352
"""Parse an OpenAI Responses API streaming event into a ChatResponseUpdate."""
23482353
metadata: dict[str, Any] = {}
@@ -2471,31 +2476,35 @@ def _parse_chunk_from_openai(
24712476
metadata.update(self._get_metadata_from_response(event))
24722477
case "response.code_interpreter_call_code.delta":
24732478
call_id = getattr(event, "call_id", None) or getattr(event, "id", None) or event.item_id
2474-
ci_additional_properties = {
2475-
"output_index": event.output_index,
2476-
"sequence_number": event.sequence_number,
2477-
"item_id": event.item_id,
2478-
}
2479-
contents.append(
2480-
Content.from_code_interpreter_tool_call(
2481-
call_id=call_id,
2482-
inputs=[
2483-
Content.from_text(
2484-
text=event.delta,
2485-
raw_representation=event,
2486-
additional_properties=ci_additional_properties,
2487-
)
2488-
],
2489-
raw_representation=event,
2490-
additional_properties=ci_additional_properties,
2479+
if code_interpreter_accumulator is not None:
2480+
code_interpreter_accumulator[call_id] = code_interpreter_accumulator.get(call_id, "") + event.delta
2481+
else:
2482+
ci_additional_properties = {
2483+
"output_index": event.output_index,
2484+
"sequence_number": event.sequence_number,
2485+
"item_id": event.item_id,
2486+
}
2487+
contents.append(
2488+
Content.from_code_interpreter_tool_call(
2489+
call_id=call_id,
2490+
inputs=[
2491+
Content.from_text(
2492+
text=event.delta,
2493+
raw_representation=event,
2494+
additional_properties=ci_additional_properties,
2495+
)
2496+
],
2497+
raw_representation=event,
2498+
additional_properties=ci_additional_properties,
2499+
)
24912500
)
2492-
)
24932501
metadata.update(self._get_metadata_from_response(event))
2494-
# NOTE: Unlike reasoning done events, code_interpreter done events always
2495-
# emit content because downstream consumers do not accumulate
2496-
# code_interpreter deltas the same way.
24972502
case "response.code_interpreter_call_code.done":
24982503
call_id = getattr(event, "call_id", None) or getattr(event, "id", None) or event.item_id
2504+
if code_interpreter_accumulator is not None and call_id in code_interpreter_accumulator:
2505+
text = code_interpreter_accumulator.pop(call_id)
2506+
else:
2507+
text = event.code
24992508
ci_additional_properties = {
25002509
"output_index": event.output_index,
25012510
"sequence_number": event.sequence_number,
@@ -2506,7 +2515,7 @@ def _parse_chunk_from_openai(
25062515
call_id=call_id,
25072516
inputs=[
25082517
Content.from_text(
2509-
text=event.code,
2518+
text=text,
25102519
raw_representation=event,
25112520
additional_properties=ci_additional_properties,
25122521
)

0 commit comments

Comments
 (0)