From f47bc67c40b396280d5c1ee2abd674f7f0474987 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:13:28 -0500 Subject: [PATCH] fix(vertex-ai): resolve a root $ref chain in linear time Walk $defs by name once with cycle detection, so a long missing ref or a long-named ref cycle beside many defs no longer costs quadratic CPU --- litellm/llms/vertex_ai/common_utils.py | 18 ++++++---- .../vertex_ai/test_vertex_ai_common_utils.py | 33 ++++++++++++++++++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/litellm/llms/vertex_ai/common_utils.py b/litellm/llms/vertex_ai/common_utils.py index 329857f94b2..b02f986eee8 100644 --- a/litellm/llms/vertex_ai/common_utils.py +++ b/litellm/llms/vertex_ai/common_utils.py @@ -940,18 +940,24 @@ def _root_cannot_be_vertex_parameters(root: Mapping[str, object]) -> bool: ) or not _is_absent_or_object_type(root.get("type")) -def _root_ref_target(schema: Mapping[str, JsonValue], defs: Mapping[str, JsonValue]) -> Mapping[str, JsonValue]: +def _root_ref_def_name(schema: Mapping[str, JsonValue], def_names: Mapping[str, str]) -> str | None: match schema.get("$ref"): case str() as ref if ref.startswith(_ROOT_DEFS_REF_PREFIX): - target: Final = defs.get(ref.removeprefix(_ROOT_DEFS_REF_PREFIX)) - return target if isinstance(target, dict) else schema + return def_names.get(ref.removeprefix(_ROOT_DEFS_REF_PREFIX)) case _: - return schema + return None def _resolve_root_ref(schema: Mapping[str, JsonValue], defs: Mapping[str, JsonValue]) -> Mapping[str, JsonValue]: - resolved: Final = reduce(lambda current, _: _root_ref_target(current, defs), range(len(defs)), schema) - return resolved if _root_ref_target(resolved, defs) is resolved else schema + object_defs: Final = MappingProxyType({name: value for name, value in defs.items() if isinstance(value, dict)}) + def_names: Final = MappingProxyType({name: name for name in object_defs}) + next_names: Final = ((name, _root_ref_def_name(value, def_names)) for name, value in object_defs.items()) + next_def: Final = MappingProxyType({name: target for name, target in next_names if target is not None}) + first: Final = _root_ref_def_name(schema, def_names) + if first is None: + return schema + last: Final = reduce(lambda name, _: next_def.get(name, name), range(len(next_def)), first) + return schema if last in next_def else object_defs[last] def needs_parameters_json_schema(parameters: Mapping[str, JsonValue]) -> bool: diff --git a/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py b/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py index 97ad953da0b..66335ae78d8 100644 --- a/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py +++ b/tests/unit/llms/vertex_ai/test_vertex_ai_common_utils.py @@ -1,7 +1,7 @@ import copy import threading import time -from collections.abc import Iterator, Mapping +from collections.abc import Callable, Iterator, Mapping from contextlib import contextmanager from enum import Enum from typing import Final @@ -1726,6 +1726,37 @@ def test_needs_parameters_json_schema(parameters: Mapping[str, JsonValue], expec assert needs_parameters_json_schema(parameters) is expected +def _missing_root_ref(size: int) -> Mapping[str, JsonValue]: + return {"$ref": "#/$defs/" + "m" * size, "$defs": {f"d{i}": {} for i in range(size // 16)}} + + +def _long_named_root_ref_cycle(size: int) -> Mapping[str, JsonValue]: + first, second = "a" * size, "b" * size + fillers: Final = {f"d{i}": {"type": "string"} for i in range(size // 16)} + cycle: Final = {first: {"$ref": f"#/$defs/{second}"}, second: {"$ref": f"#/$defs/{first}"}} + return {"$ref": f"#/$defs/{first}", "$defs": {**cycle, **fillers}} + + +def _root_ref_cost(parameters: Mapping[str, JsonValue]) -> float: + def once() -> float: + started: Final = time.perf_counter() + needs_parameters_json_schema(parameters) + return time.perf_counter() - started + + return min(once() for _ in range(3)) + + +@pytest.mark.parametrize("build", [_missing_root_ref, _long_named_root_ref_cycle], ids=["missing-ref", "ref-cycle"]) +def test_needs_parameters_json_schema_stays_linear_on_long_refs_and_many_defs( + build: Callable[[int], Mapping[str, JsonValue]], +) -> None: + """Tool parameters are caller-controlled, so 4x longer refs with 4x more $defs must not cost 16x. + Comparing two sizes keeps this honest on a slow box, and the quadratic walk this replaced grew 13x to 16x.""" + growth: Final = _root_ref_cost(build(256 * 1024)) / _root_ref_cost(build(64 * 1024)) + + assert growth < 10.0, f"cost grew {growth:.1f}x for 4x the input" + + class _Priority(int, Enum): LOW = 1 HIGH = 3