calesthio/OpenMontage · error · ValueError

text exceeds Kling TTS safety limit of 5000 characters

Error message

text exceeds Kling TTS safety limit of 5000 characters

What it means

Raised by kling_tts._build_request when the stripped text exceeds 5000 characters — Kling's official TTS API hard limit. The wrapper enforces it client-side to avoid a wasted round trip and an opaque upstream rejection.

Solutions

  1. Split text into chunks under 5000 characters at sentence boundaries and make sequential calls
  2. Use the numbered output path support (numbered_output_path) to keep chunk artifacts ordered
  3. Compute len(text.strip()) before calling and warn in the pipeline

Example fix

// before
result = tool.execute({"text": long_script, "voice_id": vid})  # raises if >5000

// after
chunks = [long_script[i:i+4800] for i in range(0, len(long_script), 4800)]
results = [tool.execute({"text": c, "voice_id": vid}) for c in chunks]
Defensive patterns

Strategy: validation

Validate before calling

text = str(inputs.get("text") or "").strip()
if len(text) > 5000:
    raise ValueError(f"text is {len(text)} chars; split into <=5000-char chunks")

Prevention

When it happens

Trigger: Long narration scripts, full articles, or concatenated segments passed as a single text value longer than 5000 chars.

Common situations: Feeding an entire blog post or voiceover script to one TTS call; merging multiple scenes into one request; CJK text where character count hits the cap faster than expected.

Related errors


AI-assisted analysis of calesthio/OpenMontage@95e1c3d0ab (2026-08-15). Data as JSON: /api/errors/db3262f257f700de. Report an issue: GitHub.

Appendix: source

Thrown at tools/audio/kling_tts.py:199

                "format": paths[0].suffix.lstrip(".") or "mp3",
                "audio_duration_seconds": round(audio_duration, 2) if audio_duration else None,
                "cost_estimate_confidence": "low",
                "cost_estimate_basis": "Conservative estimate pending official account-usage reconciliation.",
                **self._account_usage_result(inputs, client),
                **self._callback_result_data(inputs, task_id),
            },
            artifacts=[str(path) for path in paths],
            cost_usd=self.estimate_cost(inputs),
            duration_seconds=round(time.time() - start, 2),
            model="kling-official-tts",
        )

    def _build_request(self, inputs: dict[str, Any]) -> dict[str, Any]:
        text = str(inputs.get("text") or "").strip()
        if not text:
            raise ValueError("text is required")
        if len(text) > 5000:
            raise ValueError("text exceeds Kling TTS safety limit of 5000 characters")

        voice_id = str(inputs.get("voice_id") or "").strip()
        if not voice_id:
            raise ValueError("voice_id is required for Kling official TTS")

        voice_language = str(inputs.get("voice_language") or "en")
        if voice_language not in TTS_LANGUAGES:
            raise ValueError(f"voice_language must be one of: {', '.join(TTS_LANGUAGES)}")

        voice_speed = float(inputs.get("voice_speed", 1.0))
        if voice_speed < TTS_SPEED_MIN or voice_speed > TTS_SPEED_MAX:
            raise ValueError(f"voice_speed must be between {TTS_SPEED_MIN} and {TTS_SPEED_MAX}")

        payload: dict[str, Any] = {
            "text": text,
            "voice_id": voice_id,
            "voice_language": voice_language,
            "voice_speed": voice_speed,

View on GitHub (pinned to 95e1c3d0ab)