Repository navigation
fix(chat): make get_tokens() robust to non-alternating turn histories - #445
Merged
Merged
Conversation
get_tokens() computed per-turn token counts by pairing user and assistant turns positionally, which only works when turns strictly alternate. A trailing user turn with no completed assistant response (e.g. after a failed request or add_turn()), consecutive assistant turns, or a lone user turn raised ValueError. The computation is now driven off complete assistant turns: since each assistant turn's input token count is cumulative, implied user tokens fall out of consecutive differences without any alternation assumption. A history with no complete assistant turns now returns an empty list. Ports tidyverse/ellmer#1157 (fixes tidyverse/ellmer#1131).
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.
Ports tidyverse/ellmer#1157 (fixes tidyverse/ellmer#1131).
Problem
Chat.get_tokens()computed per-turn token counts by pairing user and assistant turns positionally, which only works when turns strictly alternate. It raisedValueErrorfor histories that don't:add_turn()set_turns())Fix
The computation is now driven off complete assistant turns instead of turn positions. Each assistant turn's input token count is cumulative over everything sent so far, so the implied user tokens for a round trip is simply the difference between consecutive requests — no alternation assumption needed. Histories with no complete assistant turns now return an empty list, and output for well-formed histories (including tool-calling loops, where tool results ride in user turns) is unchanged.
The only remaining
ValueErroris for assistant turns that genuinely lack token counts (e.g. fabricated viaset_turns()), pointing at.get_turns()for raw values as before.Tests
New credential-free tests cover: trailing user turn, lone user turn, consecutive assistant turns, tool loops, and the missing-token-count error. Existing
test_tokens_methodexpectations are unchanged.