BerriAI/litellm · error · ContextWindowExceededError

NLPCloudException

Error message

NLPCloudException - {error_str}

What it means

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.

Solutions

  1. Truncate or chunk input before the call - check the engine's documented character/token cap and stay under it.
  2. Pre-count size client-side (len(text) against the engine limit) and split into multiple calls.
  3. Switch to an engine with a larger input limit for long documents.
  4. Catch ContextWindowExceededError and re-dispatch with a halved chunk as a safety net.

Example fix

# before
litellm.completion(model='nlp_cloud/fastgpt', messages=[{'role':'user','content':huge_doc}])

# after
chunks = [huge_doc[i:i+5000] for i in range(0, len(huge_doc), 5000)]
outs = [litellm.completion(model='nlp_cloud/fastgpt', messages=[{'role':'user','content':c}]) for c in chunks]
Defensive patterns

Strategy: validation

Validate before calling

MAX_INPUT_CHARS = 4_000  # per NLP Cloud engine docs

def fits_nlp_cloud(text: str, limit: int = MAX_INPUT_CHARS) -> bool:
    return len(text) <= limit

Type guard

from litellm import ContextWindowExceededError

def is_input_too_long(e: BaseException) -> bool:
    return isinstance(e, ContextWindowExceededError)

Try / catch

from litellm import ContextWindowExceededError

try:
    resp = litellm.completion(model='nlp_cloud/<engine>', messages=msgs)
except ContextWindowExceededError:
    text = msgs[-1]['content']
    resp = [litellm.completion(model='nlp_cloud/<engine>', messages=[{'role': 'user', 'content': text[i:i+4000]}]) for i in range(0, len(text), 4000)]

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/712092313e317ed9. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/exception_mapping_utils.py:1568

                llm_provider="ai21",
                model=model,
                request=getattr(original_exception, "request", None),
            )


def _map_nlp_cloud_exception(
    *,
    model: str,
    original_exception: _ProviderHTTPException,
    custom_llm_provider: str,
    error_str: str,
    exception_type: str,
    exception_provider: str,
    extra_information: str,
) -> None:
    if "detail" in error_str:
        if "Input text length should not exceed" in error_str:
            raise ContextWindowExceededError(
                message=f"NLPCloudException - {error_str}",
                model=model,
                llm_provider="nlp_cloud",
                response=getattr(original_exception, "response", None),
            )
        elif "value is not a valid" in error_str:
            raise BadRequestError(
                message=f"NLPCloudException - {error_str}",
                model=model,
                llm_provider="nlp_cloud",
                response=getattr(original_exception, "response", None),
            )
        else:
            raise APIError(
                status_code=500,
                message=f"NLPCloudException - {error_str}",
                model=model,
                llm_provider="nlp_cloud",

View on GitHub (pinned to 77b7c6c40c)