{"record":{"id":"8095100ff82e29fb","repo":"langchain-ai/deepagents","slug":"chunk-limit-must-be-positive","errorCode":null,"errorMessage":"chunk limit must be positive","messagePattern":"chunk limit must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/talon/deepagents_talon/channels/base.py","lineNumber":161,"sourceCode":"    return _BOLD_PATTERN.sub(lambda match: f\"*{match.group(1) or match.group(2)}*\", value)\n\n\ndef chunk_text(text: str, *, limit: int = MAX_TEXT_CHARS) -> list[str]:\n    \"\"\"Split outbound text into channel-sized chunks.\n\n    Args:\n        text: Text to split.\n        limit: Maximum characters per returned chunk.\n\n    Returns:\n        Non-empty chunks no longer than `limit`.\n\n    Raises:\n        ValueError: If `limit` is not positive.\n    \"\"\"\n    if limit < 1:\n        msg = \"chunk limit must be positive\"\n        raise ValueError(msg)\n\n    chunks: list[str] = []\n    remaining = text\n    while len(remaining) > limit:\n        split = _split_index(remaining, limit)\n        chunk = remaining[:split].rstrip()\n        chunks.append(chunk or remaining[:limit])\n        remaining = remaining[split:].lstrip()\n    if remaining:\n        chunks.append(remaining)\n    return chunks\n\n\ndef channel_exposure_from_env(\n    env: Mapping[str, str],\n    config: ChannelExposureEnv,\n) -> ChannelExposure:\n    \"\"\"Build shared channel exposure policy from provider-specific env prefix.","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/talon/deepagents_talon/channels/base.py#L143-L179","documentation":"`chunk_text` splits outbound channel text into pieces no longer than `limit` characters and raises `ValueError` if `limit` is not positive (`< 1`). A non-positive limit would make the splitting loop meaningless (infinite loop / empty chunks), so it is rejected up front. Default callers pass the channel's `MAX_TEXT_CHARS`, so this fires mainly with custom limits.","triggerScenarios":"Calling `chunk_text(text, limit=0)`, `chunk_text(text, limit=-5)`, or invoking it through `send_message` / `_chunk_with_bot_header` with a channel or bot-header configuration whose effective limit computed to <= 0 (e.g. header length >= max message length leaving no budget).","commonSituations":"Custom channel implementations passing an uninitialized or misparsed limit env var; a max-message-length setting smaller than the bot header, driving the residual chunk limit to zero; tests probing boundary conditions.","solutions":["Pass a positive `limit` (at least 1; practically the channel's real character cap).","Fix the config/env value that produced 0 or negative — e.g. ensure the max-length env var parses to a positive integer.","If a bot header consumes the budget, increase the channel max length so `max_len - len(header) >= 1`.","Clamp before calling: `limit = max(1, configured_limit)`.","Use the default (`chunk_text(text)`) which applies `MAX_TEXT_CHARS`."],"exampleFix":"// before\nchunks = chunk_text(text, limit=0)  # ValueError\n\n// after\nchunks = chunk_text(text, limit=max(1, configured_limit))","handlingStrategy":"validation","validationCode":"def safe_chunk(text: str, limit: int) -> list[str]:\n    return chunk_text(text, limit=max(1, limit))","typeGuard":"def is_valid_chunk_limit(limit: int) -> bool:\n    return isinstance(limit, int) and limit >= 1","tryCatchPattern":"try:\n    chunks = chunk_text(text, limit=limit)\nexcept ValueError as e:\n    log.error(\"invalid chunk limit %s: %s\", limit, e)\n    chunks = chunk_text(text)  # fall back to default MAX_TEXT_CHARS","preventionTips":["When computing a residual limit (max message length minus bot header), clamp to at least 1: `max(1, max_len - len(header))`.","Validate channel max-length env vars parse to positive integers at startup.","Use the default `MAX_TEXT_CHARS` limit unless the channel genuinely requires custom sizing.","Add a boundary unit test for any custom channel's computed chunk limit."],"tags":["python","validation","valueerror","messaging","chunking"],"backgroundTag":"invalid-parameter-value","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}