BerriAI/litellm · error · ValueError

messages required for token counting

Error message

messages required for token counting

What it means

Request validation in the partner-model count_tokens handler: after version-suffix stripping, the payload has no 'messages' key. The Vertex count-tokens endpoint requires the message list to estimate tokens.

Source

Thrown at litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py:126

        Returns:
            Dict containing token count information

        Raises:
            ValueError: If required parameters are missing or invalid
        """
        # Strip version suffixes (@default, @20251001, etc.) — the Vertex AI
        # count-tokens endpoint does not accept versioned model names.
        model = self._strip_version_suffix(model)
        if "model" in request_data:
            request_data = {
                **request_data,
                "model": self._strip_version_suffix(request_data["model"]),
            }

        # Validate request
        if "messages" not in request_data:
            raise ValueError("messages required for token counting")

        # Extract Vertex AI credentials and settings
        vertex_credentials: Final = self.get_vertex_ai_credentials(litellm_params)
        vertex_project: Final = self.get_vertex_ai_project(litellm_params)

        # Check for count_tokens specific location override
        vertex_count_tokens_location: Final = litellm_params.get("vertex_count_tokens_location")
        vertex_location_raw: Final = self.get_vertex_ai_location(litellm_params)

        # Determine final location with precedence:
        # 1. vertex_count_tokens_location (if provided)
        # 2. vertex_location (if provided)
        # 3. Default to us-east5 for Claude models when no location is set
        # Supported regions: us-east5, europe-west1, asia-southeast1
        # https://docs.cloud.google.com/vertex-ai/generative-ai/docs/partner-models/claude/count-tokens
        if vertex_count_tokens_location:
            vertex_location: str = vertex_count_tokens_location
        elif vertex_location_raw:

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Pass a non-empty 'messages' list to the token counting call.
  2. Ensure the messages argument is not dropped or None before invoking count_tokens.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at litellm/llms/vertex_ai/vertex_ai_partner_models/count_tokens/handler.py:126 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/ce5daa07fd6795f8. Report an issue: GitHub.