fix: Throw TokenLimitReachedException on truncated tool calls#214
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #214 +/- ##
============================================
+ Coverage 87.45% 87.63% +0.18%
- Complexity 1194 1200 +6
============================================
Files 60 60
Lines 3858 3874 +16
============================================
+ Hits 3374 3395 +21
+ Misses 484 479 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…oken-limit-reached
…ed | Code Coverage
|
Pushed a new commit for coverage :) |
Summary
Fixes #193.
finish_reason=lengthwithtool_callspresent), throwTokenLimitReachedExceptioninstead of silently returning aFunctionCallwith null/incomplete arguments.json_last_error()check inparseResponseChoiceMessageToolCallPart()to catch malformed tool call JSON regardless of finish reason.maxTokensvalue so callers can handle gracefully (e.g. increase the limit and retry).Changes
src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModel.phpparseResponseChoiceToCandidate(): detectfinish_reason=length+tool_callspresent in raw message data and throwTokenLimitReachedExceptionbefore parsing the message.parseResponseChoiceMessageToolCallPart(): replace silentjson_decodewith an explicitjson_last_error()check that throwsResponseExceptionon invalid JSON.tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModelTest.phptestParseResponseChoiceToCandidateThrowsOnLengthWithToolCalls— truncated tool call throws exception.testParseResponseChoiceToCandidateTokenLimitIncludesMaxTokens— exception carries configured maxTokens (500).testParseResponseChoiceToCandidateLengthWithoutToolCallsDoesNotThrow— text-only + length still works.testParseResponseChoiceToCandidateToolCallsFinishReasonDoesNotThrow— normal tool calls unaffected.Test plan
vendor/bin/phpunit --filter "testParseResponseChoiceToCandidate"— all 12 tests pass.vendor/bin/phpunit— full suite passes (1059 tests, 3921 assertions).vendor/bin/phpstan analyse— no errors on source file.