{"record":{"id":"712092313e317ed9","repo":"BerriAI/litellm","slug":"nlpcloudexception-error-str","errorCode":null,"errorMessage":"NLPCloudException - {error_str}","messagePattern":"NLPCloudException - (.+?)","errorType":"exception","errorClass":"ContextWindowExceededError","httpStatus":400,"severity":"error","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1568,"sourceCode":"                llm_provider=\"ai21\",\n                model=model,\n                request=getattr(original_exception, \"request\", None),\n            )\n\n\ndef _map_nlp_cloud_exception(\n    *,\n    model: str,\n    original_exception: _ProviderHTTPException,\n    custom_llm_provider: str,\n    error_str: str,\n    exception_type: str,\n    exception_provider: str,\n    extra_information: str,\n) -> None:\n    if \"detail\" in error_str:\n        if \"Input text length should not exceed\" in error_str:\n            raise ContextWindowExceededError(\n                message=f\"NLPCloudException - {error_str}\",\n                model=model,\n                llm_provider=\"nlp_cloud\",\n                response=getattr(original_exception, \"response\", None),\n            )\n        elif \"value is not a valid\" in error_str:\n            raise BadRequestError(\n                message=f\"NLPCloudException - {error_str}\",\n                model=model,\n                llm_provider=\"nlp_cloud\",\n                response=getattr(original_exception, \"response\", None),\n            )\n        else:\n            raise APIError(\n                status_code=500,\n                message=f\"NLPCloudException - {error_str}\",\n                model=model,\n                llm_provider=\"nlp_cloud\",","sourceCodeStart":1550,"sourceCodeEnd":1586,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L1550-L1586","documentation":"This is a litellm ContextWindowExceededError raised by the NLP Cloud exception mapper. When the NLP Cloud error body contains 'detail' and the substring 'Input text length should not exceed', litellm maps it to this class, meaning the prompt (plus expected output) exceeds the model's token limit on nlp_cloud. It enables uniform context-window handling across providers.","triggerScenarios":"Calling completion(model=\"finetuned-gpt-neox-20b\", ...) or other NLP Cloud models with a prompt longer than the model's max input tokens; stuffing whole documents into messages.","commonSituations":"RAG pipelines that overload context instead of chunking; long chat histories never truncated; switching from a large-context model to a smaller NLP Cloud model without re-tuning prompt size.","solutions":["Truncate or chunk the prompt so input tokens fit the model's limit","Switch to a larger-context model on NLP Cloud if available","Use litellm's get_max_tokens(model) to check the limit before sending","Add summarization/compaction of prior conversation turns"],"exampleFix":"# before\nlitellm.completion(model=\"finetuned-gpt-neox-20b\", messages=full_history)\n\n# after\nmax_tok = litellm.get_max_tokens(\"finetuned-gpt-neox-20b\")\nlitellm.completion(\n    model=\"finetuned-gpt-neox-20b\",\n    messages=trim_to_budget(full_history, max_tok - 256),\n)","handlingStrategy":"validation","validationCode":"model = \"finetuned-gpt-neox-20b\"\nbudget = litellm.get_max_tokens(model) or 4096\nprompt_tokens = len(litellm.encode(model=model, text=\"\".join(m[\"content\"] for m in messages)))\nif prompt_tokens >= budget - 256:\n    messages = truncate_messages(messages, budget - 256)","typeGuard":"def is_context_window_error(e: Exception) -> bool:\n    return isinstance(e, litellm.ContextWindowExceededError)","tryCatchPattern":"try:\n    resp = litellm.completion(model=model, messages=messages)\nexcept litellm.ContextWindowExceededError:\n    messages = truncate_messages(messages, half=True)\n    resp = litellm.completion(model=model, messages=messages)","preventionTips":["Count tokens with litellm.encode before every long-context call","Chunk documents instead of stuffing full context","Reserve headroom for output tokens (prompt + max_tokens < limit)"],"tags":["nlp-cloud","context-window","token-limit","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}