Skip to content

Python: coalesce code interpreter history chunks#5801

Open
he-yufeng wants to merge 2 commits into
microsoft:mainfrom
he-yufeng:fix/code-interpreter-history-chunks
Open

Python: coalesce code interpreter history chunks#5801
he-yufeng wants to merge 2 commits into
microsoft:mainfrom
he-yufeng:fix/code-interpreter-history-chunks

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Fixes #5793.

Summary

  • Coalesce streamed code_interpreter_tool_call and code_interpreter_tool_result content by call_id during response finalization.
  • When a later done event carries the full code, keep that full value instead of storing both chunk deltas and the complete script.
  • Add a history-provider regression test for the Cosmos-style chunked code interpreter shape.

To verify

  • uv run pytest packages\core\tests\core\test_sessions.py::TestHistoryProviderBase::test_after_run_stores_coalesced_code_interpreter_chunks -q --basetemp .tmp\pytest
  • uv run pytest packages\core\tests\core\test_sessions.py -q --basetemp .tmp\pytest -p no:cacheprovider
  • uv run pytest packages\core\tests\core\test_types.py -q --basetemp .tmp\pytest -p no:cacheprovider
  • uv run pytest packages\openai\tests\openai\test_openai_chat_client.py -q -k "code_interpreter" --basetemp .tmp\pytest -p no:cacheprovider
  • uv run ruff check packages\core\agent_framework_types.py packages\core\tests\core\test_sessions.py
  • uv run ruff format --check packages\core\agent_framework_types.py packages\core\tests\core\test_sessions.py
  • uv run mypy packages\core\agent_framework_types.py
  • uv run python -m py_compile packages\core\agent_framework_types.py packages\core\tests\core\test_sessions.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 13, 2026 06:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses streamed code-interpreter history bloat in the Python core by coalescing code_interpreter_tool_call / code_interpreter_tool_result content items with the same call_id (or item_id) during response finalization, ensuring history providers receive a single aggregated item per logical tool call.

Changes:

  • Add response-finalization logic to coalesce code-interpreter tool call/result content by (type, call_id).
  • Implement merge behavior that prefers a later “done” event carrying the full code over keeping both deltas and the complete script.
  • Add a regression test ensuring history providers store the coalesced code-interpreter content shape.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/_types.py Adds code-interpreter coalescing/merge helpers and applies them during _finalize_response.
python/packages/core/tests/core/test_sessions.py Adds a history-provider regression test verifying coalesced code-interpreter chunks are stored as a single content item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: CosmosHistoryProvider Code interpreter tool calls are saved chunk by chunk

3 participants