{"record":{"id":"4991ac4376d006f7","repo":"BerriAI/litellm","slug":"tools-or-tool-choice-cannot-be-set-if-using-text","errorCode":null,"errorMessage":"tools or tool_choice cannot be set if using text","messagePattern":"tools or tool_choice cannot be set if using text","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/token_counter.py","lineNumber":381,"sourceCode":"\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:\n            includes_system_message: Final = any([message.get(\"role\", None) == \"system\" for message in new_messages])\n            num_tokens += _count_extra(params.count_function, tools, tool_choice, includes_system_message)\n\n    else:\n        raise ValueError(\"Either text or messages must be provided\")\n","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/token_counter.py#L363-L399","documentation":"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.","triggerScenarios":"Thrown at litellm/litellm_core_utils/token_counter.py:381 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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."],"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"}