BerriAI/litellm · error · ValueError

chunk is not a string

Error message

chunk is not a string: {chunk}

What it means

Streaming parser guard: the received chunk is not a string/bytes payload as expected (e.g. an object or None reached the string-decoding path), so parsing cannot proceed and the offending chunk is included in the error.

Solutions

  1. The provider stream returned non-string chunks; check the provider integration or model output mode.
  2. Avoid response formats the streaming parser cannot decode as text.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at litellm/litellm_core_utils/streaming_handler.py:1399 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at litellm/litellm_core_utils/streaming_handler.py:1399

            response_obj = self.handle_openai_text_completion_chunk(chunk)
            completion_obj["content"] = response_obj["text"]
            print_verbose(f"completion obj content: {completion_obj['content']}")
            if response_obj["is_finished"]:
                self.received_finish_reason = response_obj["finish_reason"]
            if response_obj["usage"] is not None:
                _text_completion_usage: Final[Usage] = response_obj["usage"]
                setattr(
                    model_response,
                    "usage",
                    litellm.Usage(
                        prompt_tokens=_text_completion_usage.prompt_tokens,
                        completion_tokens=_text_completion_usage.completion_tokens,
                        total_tokens=_text_completion_usage.total_tokens,
                    ),
                )
        elif self.custom_llm_provider == "text-completion-codestral":
            if not isinstance(chunk, str):
                raise ValueError(f"chunk is not a string: {chunk}")
            response_obj = cast(
                dict[str, Any],
                litellm.CodestralTextCompletionConfig()._chunk_parser(chunk),
            )
            completion_obj["content"] = response_obj["text"]
            print_verbose(f"completion obj content: {completion_obj['content']}")
            if response_obj["is_finished"]:
                self.received_finish_reason = response_obj["finish_reason"]
            if "usage" in response_obj is not None:
                _codestral_usage: Final[Usage] = response_obj["usage"]
                setattr(
                    model_response,
                    "usage",
                    litellm.Usage(
                        prompt_tokens=_codestral_usage.prompt_tokens,
                        completion_tokens=_codestral_usage.completion_tokens,
                        total_tokens=_codestral_usage.total_tokens,
                    ),

View on GitHub (pinned to 77b7c6c40c)