Python: Fix empty final output after tool calls#5906
Open
VIVEKJAINDTU wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes cases where tool execution succeeds but the terminal assistant turn is empty, causing response.text / final_output to be "" and breaking callers that depend on a final assistant message.
Changes:
- Update the OpenAI Chat Completions response parser to skip emitting empty assistant messages.
- Add a function-invocation fallback: if tools ran and the terminal assistant text is empty, issue one more model call with
tool_choice="none"to force a text response (unless middleware requested early termination). - Add a unit test covering recovery from an empty terminal assistant response after tool execution.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
python/packages/openai/agent_framework_openai/_chat_completion_client.py |
Skips emitting empty terminal assistant messages when parsing Chat Completions responses. |
python/packages/core/agent_framework/_tools.py |
Adds a post-tools fallback call (tool_choice="none") to recover a final text response, and threads middleware termination state. |
python/packages/core/tests/core/test_function_invocation_logic.py |
Adds a regression test for recovering from an empty terminal assistant message after tool execution. |
Comment on lines
+2450
to
+2454
| fallback_messages = ( | ||
| list(prepped_messages) + list(response.messages) | ||
| if response.conversation_id is None | ||
| else [] | ||
| ) |
Comment on lines
+701
to
+704
| # Some providers may return a terminal choice with no text/tool payload. | ||
| # Skip emitting empty assistant messages to avoid empty final outputs. | ||
| if contents: | ||
| messages.append(Message(role="assistant", contents=contents)) |
Author
|
@microsoft-github-policy-service agree |
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.
Motivation and Context
After a successful tool call, some providers return a final assistant turn with no text, tool calls, or reasoning payload. The conversation shape looks like:
In that case response.text stays empty even though tools ran correctly, which breaks callers that rely on the final assistant message.
Fixes: #5726
Description
Tests
Added test_base_client_with_function_calling_recovers_from_empty_terminal_response.
Contribution Checklist