BerriAI/litellm · error · ValueError
tools or tool_choice cannot be set if using text
Error message
tools or tool_choice cannot be set if using text
What it means
Argument validation in token_counter: token counting was requested with plain text, but tools/tool_choice were also passed. Tool definitions are only counted as part of a messages-based request, so this combination is rejected.
Solutions
- Remove tools/tool_choice when counting tokens for 'text' input; they only apply to messages.
- Convert the input to messages format if you need tool counting.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:381 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/4991ac4376d006f7.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/token_counter.py:381
#########################################################
# Flag to disable token counter
# We've gotten reports of this consuming CPU cycles,
# exposing this flag to allow users to disable
# it to confirm if this is indeed the issue
#########################################################
if litellm.disable_token_counter is True:
return 0
verbose_logger.debug("messages in token_counter: %s, text in token_counter: %s", messages, text)
if text is not None and messages is not None:
raise ValueError("text and messages cannot both be set")
if use_default_image_token_count is None:
use_default_image_token_count = False
if text is not None:
if tools or tool_choice:
raise ValueError("tools or tool_choice cannot be set if using text")
if isinstance(text, list):
text_to_count = "".join(t for t in text if isinstance(t, str))
elif isinstance(text, str):
text_to_count = text
count_function: Final = _get_count_function(model, custom_tokenizer)
num_tokens = count_function(text_to_count)
elif messages is not None:
new_messages: Final = cast(list[AllMessageValues], convert_list_message_to_dict(messages))
params: Final = _MessageCountParams(model, custom_tokenizer)
num_tokens = _count_messages(params, new_messages, use_default_image_token_count, default_token_count)
if count_response_tokens is False:
includes_system_message: Final = any([message.get("role", None) == "system" for message in new_messages])
num_tokens += _count_extra(params.count_function, tools, tool_choice, includes_system_message)
else:
raise ValueError("Either text or messages must be provided")
View on GitHub (pinned to 77b7c6c40c)