{"record":{"id":"61bc6eed7c0f3e16","repo":"BerriAI/litellm","slug":"text-and-messages-cannot-both-be-set","errorCode":null,"errorMessage":"text and messages cannot both be set","messagePattern":"text and messages cannot both be set","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":375,"sourceCode":"    default_token_count (Optional[int]): The default number of tokens to return for a message block, if an error occurs. Default is None.\n\n    Returns:\n    int: The number of tokens in the text.\n    \"\"\"\n    from litellm.utils import convert_list_message_to_dict\n\n    #########################################################\n    # Flag to disable token counter\n    # We've gotten reports of this consuming CPU cycles,\n    # exposing this flag to allow users to disable\n    # it to confirm if this is indeed the issue\n    #########################################################\n    if litellm.disable_token_counter is True:\n        return 0\n\n    verbose_logger.debug(\"messages in token_counter: %s, text in token_counter: %s\", messages, text)\n    if text is not None and messages is not None:\n        raise ValueError(\"text and messages cannot both be set\")\n    if use_default_image_token_count is None:\n        use_default_image_token_count = False\n\n    if text is not None:\n        if tools or tool_choice:\n            raise ValueError(\"tools or tool_choice cannot be set if using text\")\n        if isinstance(text, list):\n            text_to_count = \"\".join(t for t in text if isinstance(t, str))\n        elif isinstance(text, str):\n            text_to_count = text\n        count_function: Final = _get_count_function(model, custom_tokenizer)\n        num_tokens = count_function(text_to_count)\n\n    elif messages is not None:\n        new_messages: Final = cast(list[AllMessageValues], convert_list_message_to_dict(messages))\n        params: Final = _MessageCountParams(model, custom_tokenizer)\n        num_tokens = _count_messages(params, new_messages, use_default_image_token_count, default_token_count)\n        if count_response_tokens is False:","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L357-L393","documentation":"Argument validation in token_counter: both text and messages were supplied. Counting is defined for exactly one input shape at a time, so the ambiguity is rejected instead of guessing which to count.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:375 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass either 'text' or 'messages' to the token counter, not both.","Remove one of the two parameters."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}