fix(agent): default context lookup for empty model IDs
An empty/blank model id reaching get_model_context_length() can't be
meaningfully resolved — and it's worse than a miss: the endpoint
metadata fuzzy matcher ('model in key or key in model') is vacuously
true for "", so it matches an ARBITRARY catalog entry from the live
/v1/models response and returns whatever context length that entry
happens to have, persisting it under a junk '@<base_url>' cache key.
This started failing CI on main when the Nous portal catalog changed:
tests/run_agent/test_primary_runtime_restore.py constructs agents with
model='' against the live portal URL, the arbitrary match now lands on
a 32K entry, and init_agent raises the 64K-floor ValueError
(test_allowed_for_nous_anthropic_messages, red on every PR's slice).
Guard early: a blank model id falls back to DEFAULT_FALLBACK_CONTEXT
immediately, before any cache write or network probe.
Salvaged from #65515 by @whirmill (rebased onto current main; the
guard now sits after the malformed-base_url normalization added since,
and carries an explanatory comment for the fuzzy-match footgun).
Fixes the red slice on #85444, #85452 and every other open PR.
Co-authored-by: whirmill <5079591+whirmill@users.noreply.github.com>
This commit is contained in:
@@ -2624,6 +2624,19 @@ def get_model_context_length(
|
||||
except ValueError:
|
||||
base_url = ""
|
||||
|
||||
# An empty/blank model id can't be meaningfully resolved: every probe
|
||||
# below would either miss or — worse — fuzzy-match an arbitrary catalog
|
||||
# entry (the endpoint matcher's `model in key` check is vacuously true
|
||||
# for ""), returning whatever context length that random entry has and
|
||||
# persisting it under a junk "@<base_url>" cache key. Fall back to the
|
||||
# default immediately instead.
|
||||
if not str(model or "").strip():
|
||||
logger.info(
|
||||
"No model id provided for context length resolution — defaulting to %s tokens.",
|
||||
f"{DEFAULT_FALLBACK_CONTEXT:,}",
|
||||
)
|
||||
return DEFAULT_FALLBACK_CONTEXT
|
||||
|
||||
# Normalise provider-prefixed model names (e.g. "local:model-name" →
|
||||
# "model-name") so cache lookups and server queries use the bare ID that
|
||||
# local servers actually know about. Ollama "model:tag" colons are preserved.
|
||||
|
||||
@@ -285,6 +285,10 @@ class TestDefaultContextLengths:
|
||||
model, provider="kimi-coding", base_url=base_url
|
||||
) == 1_048_576
|
||||
|
||||
def test_empty_model_uses_fallback_context(self):
|
||||
assert get_model_context_length("") == DEFAULT_FALLBACK_CONTEXT
|
||||
assert get_model_context_length(None) == DEFAULT_FALLBACK_CONTEXT # type: ignore[arg-type]
|
||||
|
||||
|
||||
def test_xai_oauth_grok_build_uses_xai_models_dev_context(self):
|
||||
"""xAI OAuth should share the xAI provider metadata path.
|
||||
|
||||
Reference in New Issue
Block a user