BerriAI/litellm · error · ValueError

Either text or messages must be provided

Error message

Either text or messages must be provided

What it means

Argument validation in token_counter: neither text nor messages was provided, so there is no content to count tokens for.

Solutions

  1. Provide either 'text' or 'messages' to the token counter.
  2. Check the call site for accidentally omitted arguments.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:398 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/11cd5fc283a7326a. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/token_counter.py:398

        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")

    return num_tokens


def _count_function_call_tokens(
    key: str,
    value: Any,
    message: Mapping[str, Any],
    count_function: TokenCounterFunction,
) -> int:
    """
    Count tokens contributed by an assistant message's tool/function call payload.

    Handles both the modern `tool_calls` list and the legacy OpenAI
    `function_call` dict. Only the `arguments` string is counted (matching the
    existing tool_calls behavior); names are accounted for elsewhere via the
    tool/function definitions and `tool_choice`.
    """

View on GitHub (pinned to 77b7c6c40c)