-
Notifications
You must be signed in to change notification settings - Fork 806
FIX: warn instead of raising on reasoning-model token truncation #2166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
romanlutz
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
romanlutz:romanlutz-legendary-invention
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -296,11 +296,16 @@ def _validate_response(self, response: Any, request: MessagePiece) -> Message | | |
| request: The original request MessagePiece. | ||
|
|
||
| Returns: | ||
| None if valid, does not return Message for content filter (handled by _check_content_filter). | ||
| None when the response is valid and should be constructed normally. Returns an empty | ||
| response ``Message`` (with ``error="empty"``) when the response was truncated at the | ||
| token limit (``finish_reason="length"``) before producing any visible output, so the | ||
| run continues instead of raising. Content filter responses are handled separately by | ||
| ``_check_content_filter``. | ||
|
|
||
| Raises: | ||
| PyritException: For unexpected response structures or finish reasons. | ||
| EmptyResponseException: When the API returns an empty response. | ||
| EmptyResponseException: When the API returns an empty response that was not caused by | ||
| token-limit truncation. | ||
| """ | ||
| # Check for missing choices | ||
| if not hasattr(response, "choices") or not response.choices: | ||
|
|
@@ -320,6 +325,25 @@ def _validate_response(self, response: Any, request: MessagePiece) -> Message | | |
| # Check for at least one valid response type | ||
| has_content, has_audio, has_tool_calls = self._detect_response_content(choice.message) | ||
|
|
||
| # A "length" finish_reason means the model hit max_completion_tokens. Reasoning models spend | ||
| # tokens on hidden reasoning before emitting visible output, so a low limit can truncate the | ||
| # answer or leave it empty. This may be a deliberate configuration, so warn instead of raising. | ||
| if finish_reason == "length": | ||
| logger.warning( | ||
| "The response was truncated because it reached the token limit (finish_reason='length'). " | ||
| "Reasoning models consume tokens on hidden reasoning in addition to the visible answer, so a " | ||
| "low max_completion_tokens can truncate or empty the response. Increase max_completion_tokens " | ||
| "if you expected complete content." | ||
| ) | ||
| if not (has_content or has_audio or has_tool_calls): | ||
| return construct_response_from_request( | ||
| request=request, | ||
| response_text_pieces=[""], | ||
| response_type="text", | ||
| error="empty", | ||
| ) | ||
| return None | ||
|
Comment on lines
+338
to
+345
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right behavior? based on how I understand this, if there is any content, we would just ignore it? Also, there is no test for this currently |
||
|
|
||
| if not (has_content or has_audio or has_tool_calls): | ||
| logger.error("The chat returned an empty response (no content, audio, or tool_calls).") | ||
| raise EmptyResponseException( | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 points:
1- before this change, the _validate_response always returns
Nonein the "happy path", and never returns aMessage, so I think the return type should just beNoneand notMessage|None2- with above in mind, I think all we want to do, is consider the response valid and not raise an exception when it has a
finish_reason == 'length'... and instead_construct_message_from_response_asyncshould know how to handle such responses (this is the function called right after successful validation inopenai_target.py:457