Skip to content

fix: Throw TokenLimitReachedException on truncated tool calls#214

Closed
chloe-pomegranate wants to merge 3 commits intoWordPress:trunkfrom
chloe-pomegranate:fix/missing-required-parameters-token-limit-reached
Closed

fix: Throw TokenLimitReachedException on truncated tool calls#214
chloe-pomegranate wants to merge 3 commits intoWordPress:trunkfrom
chloe-pomegranate:fix/missing-required-parameters-token-limit-reached

Conversation

@chloe-pomegranate
Copy link
Contributor

Summary

Fixes #193.

  • When an API response hits the max token limit mid-way through generating a tool call (finish_reason=length with tool_calls present), throw TokenLimitReachedException instead of silently returning a FunctionCall with null/incomplete arguments.
  • Add a secondary json_last_error() check in parseResponseChoiceMessageToolCallPart() to catch malformed tool call JSON regardless of finish reason.
  • Exception includes the configured maxTokens value so callers can handle gracefully (e.g. increase the limit and retry).

Changes

src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModel.php

  • In parseResponseChoiceToCandidate(): detect finish_reason=length + tool_calls present in raw message data and throw TokenLimitReachedException before parsing the message.
  • In parseResponseChoiceMessageToolCallPart(): replace silent json_decode with an explicit json_last_error() check that throws ResponseException on invalid JSON.

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModelTest.php

  • testParseResponseChoiceToCandidateThrowsOnLengthWithToolCalls — 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.

Note: The Anthropic and Google providers would benefit from similar checks for their truncation signals (stop_reason=max_tokens and finishReason=MAX_TOKENS respectively).

@github-actions
Copy link

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: chloe-pomegranate <chloepomegranate@git.wordpress.org>
Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.63%. Comparing base (30a1506) to head (b706802).

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     
Flag Coverage Δ
unit 87.63% <100.00%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chloe-pomegranate
Copy link
Contributor Author

Pushed a new commit for coverage :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing Required Parameters in Tool Calls When Token Limit Reached

1 participant