The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: parse SGLang output-cap rejection wording (#83521)
chelsealong6c28f1f703bd
diff --git a/agent/model_metadata.py b/agent/model_metadata.pyindex bfdd4391833..129320417a3 100644--- a/agent/model_metadata.py+++ b/agent/model_metadata.py@@ -1305,6 +1305,12 @@ def parse_available_output_tokens_from_error(error_msg: str) -> Optional[int]:_available = int(_m_ctx_tok.group(1)) - (int(_m_chars.group(1)) + 2) // 3if _available >= 1:return _available+ # SGLang: "maximum context length of 131072 tokens. You requested a total of 132528 tokens: 66992 tokens+ # from the input messages and 65536 tokens for the completion" -> window - input (None when the input+ # alone overflows -> compress).+ _m_sglang = _sglang_window_and_input(error_lower)+ if _m_sglang and _m_sglang[0] - _m_sglang[1] >= 1:+ return _m_sglang[0] - _m_sglang[1]# vLLM: window and prompt both in TOKENS; available = window - input (None when the input alone# overflows -> compress). When max_tokens is the BINDING constraint vLLM reports "at least N input# tokens" with N == window + 1 - requested_output, so window - N == requested_output - 1 and each@@ -1331,7 +1337,7 @@ _OUTPUT_CAP_SIGNALS = (("in the output", "maximum context length"), ("requested", "output tokens"),("should be",), ("less than or equal",), ("must be",), ("exceeds model", "maximum output tokens"),("output limit",), ("maximum allowed number of output tokens",),- ("max_tokens is too large", "supports at most"),+ ("max_tokens is too large", "supports at most"), ("tokens from the input messages", "tokens for the completion"),)_INPUT_OVERFLOW_SIGNALS = ("prompt is too long", "prompt too long", "input is too long", "input token",@@ -1347,10 +1353,17 @@ _PARSEABLE_OUTPUT_CAP_SIGNALS = (("maximum context length", "requested", "output tokens"),("range of max_tokens should be",), ("exceeds model", "maximum output tokens"),("output limit",), ("max_tokens", "maximum allowed number of output tokens"),- ("max_tokens is too large", "supports at most"),+ ("max_tokens is too large", "supports at most"), ("tokens from the input messages", "tokens for the completion"),)+def _sglang_window_and_input(error_lower: str) -> Optional[Tuple[int, int]]:+ """``(window, input_tokens)`` from SGLang's wording, else None; both figures are explicit there."""+ _m_ctx = re.search(r'maximum context length of (\d+)\s*token', error_lower)+ _m_in = re.search(r'(\d+)\s*tokens from the input messages', error_lower)+ return (int(_m_ctx.group(1)), int(_m_in.group(1))) if _m_ctx and _m_in else None++def _any_phrase_group(text: str, groups: tuple) -> bool:return any(all(p in text for p in group) for group in groups)@@ -1361,10 +1374,13 @@ def is_output_cap_error(error_msg: str) -> bool:rejection). Signal: talks about max_tokens as a cap/range/limit and NOT about an oversized input."""error_lower = error_msg.lower()# An error that ALSO describes an oversized INPUT is a genuine overflow — compression can fix it.+ # SGLang states both figures: input >= window is that same genuine overflow.+ _m_sglang = _sglang_window_and_input(error_lower)return (- any(p in error_lower for p in ("max_tokens", "max_output_tokens", "max_completion_tokens"))+ any(p in error_lower for p in ("max_tokens", "max_output_tokens", "max_completion_tokens", "tokens for the completion"))and _any_phrase_group(error_lower, _OUTPUT_CAP_SIGNALS)and not any(p in error_lower for p in _INPUT_OVERFLOW_SIGNALS)+ and not (_m_sglang and _m_sglang[1] >= _m_sglang[0]))