{"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/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/exception_mapping_utils.py#L1550-L1586","documentation":"In _map_nlp_cloud_exception, an NLP Cloud error whose body contains 'detail' plus 'Input text length should not exceed' is re-raised as LiteLLM's ContextWindowExceededError. It is NLP Cloud's way of saying the input surpassed the model's maximum input length.","triggerScenarios":"completion('nlp_cloud/<engine>', ...) with an input longer than the engine's cap (e.g. FastGPT/Fast-base limits); NLP Cloud returns a detail body and this branch maps it.","commonSituations":"Sending whole documents instead of chunks, no truncation layer before enrichment/generation calls, or switching NLP Cloud engines to one with a smaller input limit.","solutions":["Truncate or chunk input before the call - check the engine's documented character/token cap and stay under it.","Pre-count size client-side (len(text) against the engine limit) and split into multiple calls.","Switch to an engine with a larger input limit for long documents.","Catch ContextWindowExceededError and re-dispatch with a halved chunk as a safety net."],"exampleFix":"# before\nlitellm.completion(model='nlp_cloud/fastgpt', messages=[{'role':'user','content':huge_doc}])\n\n# after\nchunks = [huge_doc[i:i+5000] for i in range(0, len(huge_doc), 5000)]\nouts = [litellm.completion(model='nlp_cloud/fastgpt', messages=[{'role':'user','content':c}]) for c in chunks]","handlingStrategy":"validation","validationCode":"MAX_INPUT_CHARS = 4_000  # per NLP Cloud engine docs\n\ndef fits_nlp_cloud(text: str, limit: int = MAX_INPUT_CHARS) -> bool:\n    return len(text) <= limit","typeGuard":"from litellm import ContextWindowExceededError\n\ndef is_input_too_long(e: BaseException) -> bool:\n    return isinstance(e, ContextWindowExceededError)","tryCatchPattern":"from litellm import ContextWindowExceededError\n\ntry:\n    resp = litellm.completion(model='nlp_cloud/<engine>', messages=msgs)\nexcept ContextWindowExceededError:\n    text = msgs[-1]['content']\n    resp = [litellm.completion(model='nlp_cloud/<engine>', messages=[{'role': 'user', 'content': text[i:i+4000]}]) for i in range(0, len(text), 4000)]","preventionTips":["Enforce engine-specific input caps before dispatch.","Build chunking into document pipelines by default.","Catch ContextWindowExceededError as a safety net that halves chunk size once."],"tags":["nlp-cloud","context-window","input-length","litellm","chunking"],"backgroundTag":"context-length-exceeded","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}