Python: fix CosmosHistoryProvider saves code interpreter tool calls chunk by chunk (#5793)#6192
Open
hanhan761 wants to merge 2 commits into
Open
Python: fix CosmosHistoryProvider saves code interpreter tool calls chunk by chunk (#5793)#6192hanhan761 wants to merge 2 commits into
hanhan761 wants to merge 2 commits into
Conversation
…ider (microsoft#5793) During streaming, code interpreter output arrives as multiple content items with the same call_id but different sequence_number values. Before persisting to Cosmos DB, consecutive code_interpreter_tool_call chunks with matching call_id are now merged into a single Content item with the complete, aggregated text.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds aggregation logic to CosmosHistoryProvider so that streamed code_interpreter_tool_call chunks (which arrive as multiple Content items sharing the same call_id) are merged into a single content item before being persisted to Cosmos DB.
Changes:
- Introduces
_merge_code_interpreter_chunkshelper andCosmosHistoryProvider._aggregate_code_interpreter_callsstatic method. - Invokes aggregation inside
save_messagesprior to serializing each message. - Adds unit tests covering merging, non-merging of distinct
call_ids, and end-to-end behavior throughsave_messages.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| python/packages/azure-cosmos/agent_framework_azure_cosmos/_history_provider.py | Implements chunk merging helper and aggregation hook in save_messages. |
| python/packages/azure-cosmos/tests/test_cosmos_history_provider.py | Adds TestCodeInterpreterAggregation test class verifying merge logic. |
Comment on lines
+239
to
+240
| if message.contents: | ||
| message.contents = self._aggregate_code_interpreter_calls(message.contents) |
Comment on lines
+31
to
+34
| for chunk in chunks: | ||
| for inp in (chunk.inputs or []): | ||
| if inp.type == "text" and inp.text: | ||
| all_text_parts.append(inp.text) |
Comment on lines
+35
to
+36
| if chunk.additional_properties: | ||
| merged_additional_properties.update(chunk.additional_properties) |
Comment on lines
+487
to
+491
| def test_save_messages_calls_aggregation(self) -> None: | ||
| """save_messages aggregates code interpreter chunks before saving.""" | ||
| from unittest.mock import AsyncMock, patch, MagicMock | ||
| from agent_framework import Content, Message | ||
| from agent_framework_azure_cosmos._history_provider import CosmosHistoryProvider |
…st style - save_messages: build message dict directly instead of mutating caller's Message\n- _merge_code_interpreter_chunks: preserve non-text inputs, drop sequence_number\n- Tests: module-level imports, async test methods, add coverage for new behaviors
Author
|
All 4 review comments have been addressed in commit 2ae1e66:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix #5793: During streaming, code interpreter tool calls are received as multiple content items with the same \call_id\ but different \sequence_number\ values. Before persisting to Cosmos DB, consecutive \code_interpreter_tool_call\ chunks with matching \call_id\ are now merged into a single \Content\ item with the complete, aggregated text.
Changes
\python/packages/azure-cosmos/agent_framework_azure_cosmos/_history_provider.py\
\python/packages/azure-cosmos/tests/test_cosmos_history_provider.py\
Added \TestCodeInterpreterAggregation\ class with tests:
Issue
Fixes #5793