{"record":{"id":"fae65452592b0802","repo":"sgl-project/sglang","slug":"prompt-has-num-text-tokens-tokens-exceeds-max-t","errorCode":null,"errorMessage":"prompt has {num_text_tokens} tokens, exceeds max_text_tokens={max_text_tokens}","messagePattern":"prompt has (.+?) tokens, exceeds max_text_tokens=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ideogram.py","lineNumber":136,"sourceCode":"\nclass Ideogram4TextEncodingStage(TextEncodingStage):\n    deduplicated_extra_tensor_tree_output_keys = (\"ideogram4\",)\n\n    def __init__(self, text_encoder, tokenizer) -> None:\n        super().__init__([text_encoder], [tokenizer])\n\n    def _tokenize(self, prompt: str, max_text_tokens: int):\n        messages = [{\"role\": \"user\", \"content\": [{\"type\": \"text\", \"text\": prompt}]}]\n        text = self.tokenizers[0].apply_chat_template(\n            messages, add_generation_prompt=True, tokenize=False\n        )\n        encoded = self.tokenizers[0](\n            text, return_tensors=\"pt\", add_special_tokens=False\n        )\n        token_ids = encoded[\"input_ids\"][0]\n        num_text_tokens = int(token_ids.shape[0])\n        if num_text_tokens > max_text_tokens:\n            raise ValueError(\n                f\"prompt has {num_text_tokens} tokens, exceeds max_text_tokens={max_text_tokens}\"\n            )\n        return token_ids, num_text_tokens\n\n    def _build_inputs(self, prompts: list[str], height: int, width: int, server_args):\n        cfg = server_args.pipeline_config\n        tokenized = [self._tokenize(p, cfg.max_text_tokens) for p in prompts]\n        batch_size = len(prompts)\n        patch = cfg.patch_size * cfg.ae_scale_factor\n        if height < 256 or height > 2048 or width < 256 or width > 2048:\n            raise ValueError(\"height/width must be between 256 and 2048\")\n        if height % patch != 0 or width % patch != 0:\n            raise ValueError(\n                f\"height/width must be divisible by patch_size*ae_scale_factor={patch}\"\n            )\n        grid_h = height // patch\n        grid_w = width // patch\n        num_image_tokens = grid_h * grid_w","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/ideogram.py#L118-L154","documentation":"Raised by the Ideogram stage's _tokenize when the tokenized prompt exceeds the pipeline config's max_text_tokens cap. The stage tokenizes text with HF tokenizers and enforces the model's context budget before building inputs. It protects the downstream transformer from overflowing its text token positions.","triggerScenarios":"Calling the Ideogram generate/forward path with a prompt whose token count (add_special_tokens=False) exceeds server_args.pipeline_config.max_text_tokens; longer prompts or batch entries with verbose descriptions trigger it.","commonSituations":"Passing very long descriptive prompts, concatenated style tags, or programmatically generated prompt strings; lowering max_text_tokens in pipeline_config; switching to a tokenizer with a larger vocabulary producing more tokens per word.","solutions":["Shorten the prompt below max_text_tokens","Raise cfg.max_text_tokens in pipeline_config if the model supports a larger text budget","Pre-tokenize and truncate/summarize the prompt before submitting"],"exampleFix":"// before\nresp = pipeline.generate(prompt=very_long_prompt)\n// after\nids = tokenizer(very_long_prompt, add_special_tokens=False)['input_ids']\nif len(ids) > cfg.max_text_tokens:\n    prompt = tokenizer.decode(ids[:cfg.max_text_tokens])\nresp = pipeline.generate(prompt=prompt)","handlingStrategy":"validation","validationCode":"ids = tokenizer(prompt, add_special_tokens=False)['input_ids']\nif len(ids) > cfg.max_text_tokens:\n    prompt = tokenizer.decode(ids[:cfg.max_text_tokens])","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pre-tokenize and measure prompts before submission","Centralize prompt length checks in the request-building layer","Document max_text_tokens for API consumers"],"tags":["ideogram","prompt-length","token-limit","validation"],"backgroundTag":"prompt-exceeds-token-limit","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}