diff --git a/litellm/llms/anthropic/messages/adapters/transformation.py b/litellm/llms/anthropic/messages/adapters/transformation.py index 50fe29b6b1b..660c7c1dbc9 100644 --- a/litellm/llms/anthropic/messages/adapters/transformation.py +++ b/litellm/llms/anthropic/messages/adapters/transformation.py @@ -20,9 +20,9 @@ from litellm.llms.anthropic.messages.utils import ( is_reasoning_auto_summary_enabled, json_copy_or_original, object_mapping, + openai_strict_json_schema, prompt_cache_key_from_user_id, ) -from litellm.llms.base_llm.base_utils import as_json_object, strict_json_schema from litellm.responses.utils import auto_reasoning_summary # OpenAI has a 64-character limit for function/tool names @@ -1022,7 +1022,7 @@ class LiteLLMAnthropicMessagesAdapter: if output_format_object is None or output_format_object.get("type") != "json_schema": return None - schema: Final = as_json_object(json_copy_or_original(output_format_object.get("schema"))) + schema: Final = output_format_object.get("schema") if not schema: return None @@ -1030,7 +1030,7 @@ class LiteLLMAnthropicMessagesAdapter: "type": "json_schema", "json_schema": { "name": "structured_output", - "schema": strict_json_schema(dict(schema)), + "schema": openai_strict_json_schema(schema), "strict": True, }, } diff --git a/litellm/llms/anthropic/messages/native/handler.py b/litellm/llms/anthropic/messages/native/handler.py index 7ad9ea31339..58735c10b4e 100644 --- a/litellm/llms/anthropic/messages/native/handler.py +++ b/litellm/llms/anthropic/messages/native/handler.py @@ -724,7 +724,7 @@ def anthropic_messages_handler( tools: list[dict] | None = None, top_k: int | None = None, top_p: float | None = None, - container: dict[str, object] | None = None, + container: str | dict[str, object] | None = None, api_key: str | None = None, api_base: str | None = None, client: AsyncHTTPHandler | None = None, diff --git a/litellm/llms/anthropic/messages/responses_adapters/handler.py b/litellm/llms/anthropic/messages/responses_adapters/handler.py index 52d1774b26e..9b77ca3ab27 100644 --- a/litellm/llms/anthropic/messages/responses_adapters/handler.py +++ b/litellm/llms/anthropic/messages/responses_adapters/handler.py @@ -311,7 +311,7 @@ def _build_responses_kwargs( output_format: AnthropicOutputSchema | None = None, speed: str | None = None, mcp_servers: Sequence[AnthropicMcpServerTool] | None = None, - container: Mapping[str, object] | None = None, + container: str | Mapping[str, object] | None = None, extra_kwargs: Mapping[str, object] | None = None, ) -> dict[str, object]: """ @@ -382,7 +382,7 @@ def _build_responses_kwargs( if sent_mcp_servers: request_data["mcp_servers"] = list(sent_mcp_servers) if container is not None: - request_data["container"] = dict(container) + request_data["container"] = container if isinstance(container, str) else dict(container) anthropic_request: Final = AnthropicMessagesRequest(**request_data) responses_kwargs: Final = _ADAPTER.translate_request( @@ -551,7 +551,7 @@ class LiteLLMMessagesToResponsesAPIHandler: output_format: AnthropicOutputSchema | None = None, speed: str | None = None, mcp_servers: Sequence[AnthropicMcpServerTool] | None = None, - container: Mapping[str, object] | None = None, + container: str | Mapping[str, object] | None = None, **kwargs: object, ) -> AnthropicMessagesResponse | AsyncIterator[bytes]: native_context_management, polyfilled_edits = _split_context_management(context_management, kwargs) @@ -627,7 +627,7 @@ class LiteLLMMessagesToResponsesAPIHandler: output_format: AnthropicOutputSchema | None = None, speed: str | None = None, mcp_servers: Sequence[AnthropicMcpServerTool] | None = None, - container: Mapping[str, object] | None = None, + container: str | Mapping[str, object] | None = None, _is_async: bool = False, **kwargs: object, ) -> ( diff --git a/litellm/llms/anthropic/messages/responses_adapters/transformation.py b/litellm/llms/anthropic/messages/responses_adapters/transformation.py index 46ce254f6af..b9a486c83b5 100644 --- a/litellm/llms/anthropic/messages/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/messages/responses_adapters/transformation.py @@ -37,9 +37,10 @@ from litellm.llms.anthropic.messages.native.utils import ( from litellm.llms.anthropic.messages.utils import ( is_reasoning_auto_summary_enabled, json_copy_or_original, + openai_strict_json_schema, prompt_cache_key_from_user_id, ) -from litellm.llms.base_llm.base_utils import as_json_object, strict_json_schema +from litellm.llms.base_llm.base_utils import as_json_object from litellm.types.llms.anthropic import ( AllAnthropicPassThroughMessageValues, AllAnthropicToolsValues, @@ -70,6 +71,7 @@ RESPONSES_INCLUDE_ENCRYPTED_REASONING: Final = "reasoning.encrypted_content" _DECODED_JSON: Final = TypeAdapter(object) _MCP_TOOLSET_TYPE: Final = "mcp_toolset" _MCP_TOOLSET: Final = TypeAdapter(AnthropicMcpToolset) +_MCP_SERVER: Final = TypeAdapter(AnthropicMcpServerTool) _WEB_SEARCH_FILTER_KEYS: Final = ("allowed_domains", "blocked_domains") _STRUCTURED_OUTPUT_NAME: Final = "structured_output" _OBJECT_MAP: Final = TypeAdapter(Mapping[str, object]) @@ -209,7 +211,7 @@ def _is_mcp_toolset(tool: Mapping[str, object]) -> bool: return tool.get("type") == _MCP_TOOLSET_TYPE -def _requested_mcp_servers(request: AnthropicMessagesRequest) -> Sequence[AnthropicMcpServerTool]: +def _requested_mcp_servers(request: AnthropicMessagesRequest) -> Sequence[object]: match request: case {"mcp_servers": list() as servers}: return servers @@ -217,10 +219,14 @@ def _requested_mcp_servers(request: AnthropicMessagesRequest) -> Sequence[Anthro return () -def _mcp_server_translation(server: AnthropicMcpServerTool, tools: Sequence[Mapping[str, object]]) -> _ResponsesTools: +def _mcp_server_translation(requested: object, tools: Sequence[Mapping[str, object]]) -> _ResponsesTools: """One Responses ``mcp`` tool per server, its access read from the server's ``mcp_toolset`` entry in ``tools``, else from the deprecated ``tool_configuration``.""" - name: Final = server.get("name") + try: + server: Final = _MCP_SERVER.validate_python(requested) + except ValidationError: + return _ResponsesTools(unsupported=("a malformed mcp_servers entry",)) + name: Final = server["name"] toolset: Final = next( (tool for tool in tools if _is_mcp_toolset(tool) and tool.get("mcp_server_name") == name), None, @@ -298,7 +304,7 @@ def _text_format(output_format: Mapping[str, object] | None) -> _Translation: strict: Final = { "type": "json_schema", "name": _STRUCTURED_OUTPUT_NAME, - "schema": strict_json_schema(dict(strict_schema)), + "schema": openai_strict_json_schema(strict_schema), "strict": True, } return _Translation(fields={"text": {"format": strict}}) diff --git a/litellm/llms/anthropic/messages/utils.py b/litellm/llms/anthropic/messages/utils.py index 66946f30ef2..94f81eadb3b 100644 --- a/litellm/llms/anthropic/messages/utils.py +++ b/litellm/llms/anthropic/messages/utils.py @@ -1,6 +1,7 @@ +import copy import json import os -from collections.abc import Mapping +from collections.abc import Mapping, Sequence from types import MappingProxyType from typing import TYPE_CHECKING, Final, TypeVar @@ -37,6 +38,61 @@ def json_copy_or_original(value: _JsonValue) -> _JsonValue: return value +_SUBSCHEMA_LIST_KEYWORDS: Final = ("anyOf", "oneOf", "allOf") +_SUBSCHEMA_MAP_KEYWORDS: Final = ("$defs", "definitions") + + +_JSON_ARRAY: Final = TypeAdapter(list[object]) + + +def _json_array(value: object) -> Sequence[object] | None: + try: + return _JSON_ARRAY.validate_python(value, strict=True) + except ValidationError: + return None + + +def openai_strict_json_schema(schema: object) -> object: + """New copy of a JSON schema where every object with properties forbids extra keys and requires all of them. + + OpenAI strict structured outputs rejects any schema that leaves an object open or a property optional. + """ + source: Final = object_mapping(schema) + if source is None: + return copy.deepcopy(schema) + properties: Final = object_mapping(source.get("properties")) + closing: Final[Mapping[str, object]] = ( + {"required": list(properties), "additionalProperties": False} + if properties is not None and _is_object_type(source.get("type")) + else {} + ) + return {**{key: _strict_subschema(key, value) for key, value in source.items()}, **closing} + + +def _is_object_type(schema_type: object) -> bool: + """Whether a ``type`` keyword names an object, alone or in a union such as ``["object", "null"]``.""" + types: Final = _json_array(schema_type) + return schema_type == "object" if types is None else "object" in types + + +def _strict_subschema(key: str, value: object) -> object: + if key == "items": + return openai_strict_json_schema(value) + if key == "properties" or key in _SUBSCHEMA_MAP_KEYWORDS: + named: Final = object_mapping(value) + return ( + copy.deepcopy(value) + if named is None + else {name: openai_strict_json_schema(subschema) for name, subschema in named.items()} + ) + if key in _SUBSCHEMA_LIST_KEYWORDS: + variants: Final = _json_array(value) + return ( + copy.deepcopy(value) if variants is None else [openai_strict_json_schema(variant) for variant in variants] + ) + return copy.deepcopy(value) + + class _ClaudeCodeUserId(LiteLLMBaseModel): """The JSON Claude Code packs into ``metadata.user_id``; only ``session_id`` is per conversation.""" diff --git a/litellm/llms/base_llm/base_utils.py b/litellm/llms/base_llm/base_utils.py index 2c140f0ffd3..b5e617499b1 100644 --- a/litellm/llms/base_llm/base_utils.py +++ b/litellm/llms/base_llm/base_utils.py @@ -21,7 +21,7 @@ from pydantic import BaseModel, JsonValue, PositiveFloat, TypeAdapter, Validatio from typing_extensions import TypeIs # noqa: TID251 # narrows a message to its system member by the role literal from litellm._logging import verbose_logger -from litellm.constants import ANTHROPIC_BILLING_METADATA_PREFIX, DEFAULT_MAX_RECURSE_DEPTH +from litellm.constants import ANTHROPIC_BILLING_METADATA_PREFIX from litellm.litellm_core_utils.core_helpers import as_str_mapping from litellm.types.llms.openai import ( AllMessageValues, @@ -228,48 +228,6 @@ def as_json_object(value: object) -> Mapping[str, JsonValue] | None: return None -_SCHEMA_MAP_KEYWORDS: Final = frozenset({"properties", "$defs", "definitions", "patternProperties"}) -_SCHEMA_DATA_KEYWORDS: Final = frozenset({"enum", "const", "examples"}) - - -def _strict_keyword_value(keyword: str, value: JsonValue, depth: int) -> JsonValue: - if keyword in _SCHEMA_DATA_KEYWORDS: - return value - if keyword in _SCHEMA_MAP_KEYWORDS and isinstance(value, dict): - return {name: strict_json_schema(subschema, depth) for name, subschema in value.items()} - return strict_json_schema(value, depth) - - -def strict_json_schema(schema: JsonValue, depth: int = 0) -> JsonValue: - """The schema as OpenAI strict structured outputs accept it: every object closed with - `additionalProperties: false` and all of its properties required, `default` removed, and only `$ref` kept on a - reference. Output that matches the result also matches the original. Raises ValueError past - DEFAULT_MAX_RECURSE_DEPTH.""" - if depth > DEFAULT_MAX_RECURSE_DEPTH: - raise ValueError(f"output schema is nested deeper than {DEFAULT_MAX_RECURSE_DEPTH} levels") - if isinstance(schema, list): - return [strict_json_schema(entry, depth + 1) for entry in schema] - if not isinstance(schema, dict): - return schema - if "$ref" in schema: - return {"$ref": schema["$ref"]} - walked: Final = { - keyword: _strict_keyword_value(keyword, value, depth + 1) - for keyword, value in schema.items() - if keyword != "default" - } - properties: Final = walked.get("properties") - if walked.get("type") != "object" and not isinstance(properties, dict): - return walked - closed_properties: Final = properties if isinstance(properties, dict) else dict[str, JsonValue]() - return { - **walked, - "properties": closed_properties, - "required": list(closed_properties), - "additionalProperties": False, - } - - @dataclass(frozen=True, slots=True) class SourceNumber: text: str diff --git a/litellm/llms/base_llm/harness/utils.py b/litellm/llms/base_llm/harness/utils.py index 85102bb30d0..7080eb01ac1 100644 --- a/litellm/llms/base_llm/harness/utils.py +++ b/litellm/llms/base_llm/harness/utils.py @@ -7,13 +7,19 @@ import json import os import typing from collections.abc import Iterator, Mapping, Sequence -from typing import Final +from types import MappingProxyType +from typing import Final, TypeAlias from pydantic import TypeAdapter, ValidationError +from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH + if typing.TYPE_CHECKING: from litellm.harness.context import SessionContext +# A decoded JSON document: what json.loads / model_json_schema() produce. +JSONValue: TypeAlias = "dict[str, JSONValue] | list[JSONValue] | str | int | float | bool | None" + SKILL_MANIFEST: Final = "SKILL.md" _JSON_DECODER: Final = json.JSONDecoder() _JSON_OBJECT_ADAPTER: Final = TypeAdapter(dict[str, object]) @@ -80,6 +86,35 @@ def turn_prompt(ctx: SessionContext, prompt: str) -> str: return f"{prompt}\n\n{structured_output_instruction(ctx.output.model_json_schema())}" +def strict_json_schema(schema: JSONValue, depth: int = 0) -> JSONValue: + """Make a JSON schema acceptable to OpenAI strict structured outputs. + + Every object gets `additionalProperties: false` and all of its properties required, + recursively. Keywords strict mode rejects next to $ref are dropped. Nesting deeper than + DEFAULT_MAX_RECURSE_DEPTH raises instead of recursing further. + """ + if depth > DEFAULT_MAX_RECURSE_DEPTH: + raise ValueError(f"output schema is nested deeper than {DEFAULT_MAX_RECURSE_DEPTH} levels") + if isinstance(schema, list): + return [strict_json_schema(entry, depth + 1) for entry in schema] # mutable-ok: JSON document output + if not isinstance(schema, dict): + return schema + entries: Final = ((key, strict_json_schema(value, depth + 1)) for key, value in schema.items()) + result = dict(entries) # mutable-ok: JSON document; "default" is popped below + if "$ref" in result: + return {"$ref": result["$ref"]} # mutable-ok: JSONValue output is a plain JSON document + result.pop("default", None) + properties = result.get("properties") + if result.get("type") == "object" or isinstance(properties, dict): + props = properties if isinstance(properties, dict) else {} # mutable-ok: JSONValue object member + required: Final[list[JSONValue]] = list(props) # mutable-ok: JSON array in the output schema + strict: Final[Mapping[str, JSONValue]] = MappingProxyType( + {"properties": props, "required": required, "additionalProperties": False} + ) + result = {**result, **strict} # mutable-ok: JSON document output + return result + + def decode_json_line(line: bytes | str) -> Mapping[str, object] | None: """One JSONL line as a dict, or None for blank / non-JSON / non-object lines.""" text = line.strip() diff --git a/litellm/llms/codex/harness/transformation.py b/litellm/llms/codex/harness/transformation.py index 1e959642b8c..d7388bd23fc 100644 --- a/litellm/llms/codex/harness/transformation.py +++ b/litellm/llms/codex/harness/transformation.py @@ -32,7 +32,6 @@ from litellm.harness.types import ( ToolResult, ) from litellm.litellm_core_utils.core_helpers import iter_items -from litellm.llms.base_llm.base_utils import strict_json_schema from litellm.llms.base_llm.harness.transformation import ( BaseCLIHarnessConfig, HarnessSessionSetup, @@ -41,7 +40,7 @@ from litellm.llms.base_llm.harness.transformation import ( HarnessTurnResponse, event_list, ) -from litellm.llms.base_llm.harness.utils import stderr_tail_text +from litellm.llms.base_llm.harness.utils import stderr_tail_text, strict_json_schema if TYPE_CHECKING: from litellm.harness.context import SessionContext diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index 68944ef38b5..a585b690ef5 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -487,7 +487,7 @@ class AnthropicMessagesRequestOptionalParams(TypedDict, total=False): mcp_servers: list[AnthropicMcpServerTool] | None context_management: dict[str, object] | None compaction: ReadOnly[AnthropicCompaction | None] - container: dict[str, object] | None # Container config with skills for code execution + container: str | dict[str, object] | None # Container id, or config with skills for code execution output_format: AnthropicOutputSchema | None # Structured outputs support speed: str | None # Fast mode support for Opus models output_config: AnthropicOutputConfig | None # Configuration for Claude's output behavior diff --git a/tests/code_coverage_tests/recursive_detector.py b/tests/code_coverage_tests/recursive_detector.py index edd1c195af9..fc153ee9447 100644 --- a/tests/code_coverage_tests/recursive_detector.py +++ b/tests/code_coverage_tests/recursive_detector.py @@ -75,7 +75,7 @@ IGNORE_FUNCTIONS = [ "_iter_fallback_targets", # max depth set (2 * ROUTER_MAX_FALLBACKS); fails closed by raising ValueError at the cap. "_mergeable_branch", # max depth set (_MAX_SCHEMA_FLATTEN_DEPTH=32) plus a seen_refs cycle guard; passes the schema through untouched at the cap. "json_string_leaves", # max depth set (MAX_STRUCTURED_CONTENT_SCAN_DEPTH); fails closed by raising at the cap so nothing goes unscanned. - "strict_json_schema", # max depth set (DEFAULT_MAX_RECURSE_DEPTH); fails closed by raising ValueError at the cap. + "strict_json_schema", # harness: max depth set (DEFAULT_MAX_RECURSE_DEPTH); fails closed by raising ValueError at the cap. "toml_value", # harness/codex: max depth set (DEFAULT_MAX_RECURSE_DEPTH); fails closed by raising OptionsMismatch at the cap. "with_json_string_leaves", # transitively bounded: only runs on a tree json_string_leaves already walked under the cap. "_clean_extraction", # ScaleDown: traverses the entity map built under MAX_SCHEMA_DEPTH=32 and MAX_ENTITIES=1000. diff --git a/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py b/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py index c54dbd89279..a76d5e82a37 100644 --- a/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py +++ b/tests/unit/llms/anthropic/messages/adapters/test_anthropic_messages_adapters_transformation.py @@ -4194,29 +4194,6 @@ class TestTranslateAnthropicOutputFormatToOpenAI: assert schema["additionalProperties"] is False assert sorted(schema["required"]) == ["age", "email", "name"] - def test_keywords_strict_mode_rejects_are_dropped_and_property_names_kept(self): - output_format = { - "type": "json_schema", - "schema": { - "type": "object", - "properties": { - "default": {"type": "string", "default": "x"}, - "extra": {"type": "object"}, - }, - }, - } - result = self.adapter.translate_anthropic_output_format_to_openai(output_format) - assert result is not None - assert result["json_schema"]["schema"] == { - "type": "object", - "properties": { - "default": {"type": "string"}, - "extra": {"type": "object", "properties": {}, "required": [], "additionalProperties": False}, - }, - "required": ["default", "extra"], - "additionalProperties": False, - } - def test_invalid_output_format_returns_none(self): assert ( self.adapter.translate_anthropic_output_format_to_openai("invalid") is None diff --git a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_handler.py b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_handler.py index 1187a2d82dd..94ca60aaa16 100644 --- a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_handler.py +++ b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_handler.py @@ -854,14 +854,23 @@ def test_speed_fast_asks_for_the_priority_tier_unless_a_service_tier_is_forwarde assert responses_kwargs["service_tier"] == service_tier -def test_speed_mcp_servers_and_container_never_reach_the_responses_call_raw() -> None: +CONTAINERS: Final = ( + pytest.param("container_abc", id="container-id"), + pytest.param({"id": "container_abc"}, id="container-params"), +) + + +@pytest.mark.parametrize("container", CONTAINERS) +def test_speed_mcp_servers_and_container_never_reach_the_responses_call_raw( + container: str | dict[str, object], +) -> None: responses_kwargs: Final = _build_responses_kwargs( model="openai/gpt-5.6", max_tokens=1024, messages=MESSAGES, speed="fast", mcp_servers=[DOCS_SERVER], - container={"id": "container_abc"}, + container=container, extra_kwargs={**OPENAI_API, "drop_params": True}, ) assert {"speed", "mcp_servers", "container"}.isdisjoint(responses_kwargs) @@ -875,13 +884,14 @@ def test_speed_mcp_servers_and_container_never_reach_the_responses_call_raw() -> ] -def test_container_answers_400_without_drop_params() -> None: +@pytest.mark.parametrize("container", CONTAINERS) +def test_container_answers_400_without_drop_params(container: str | dict[str, object]) -> None: with pytest.raises(litellm.UnsupportedParamsError) as rejected: _build_responses_kwargs( model="openai/gpt-5.6", max_tokens=1024, messages=MESSAGES, - container={"id": "container_abc"}, + container=container, extra_kwargs=OPENAI_API, ) diff --git a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py index 814b8215a86..ccbcf3acaf8 100644 --- a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py +++ b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py @@ -243,7 +243,7 @@ class TestOutputConfigStructuredOutput: "type": "object", "properties": { "name": {"type": "string"}, - "nickname": {"type": "string"}, + "nickname": {"type": "string", "default": "none"}, "address": { "type": "object", "properties": {"city": {"type": "string"}}, @@ -280,6 +280,39 @@ class TestOutputConfigStructuredOutput: kwargs = _ADAPTER.translate_request(_make_request(**fields)) assert kwargs.get("text", {}).get("format") == expected_format + _ANSWER: Final = {"type": "object", "properties": {"text": {"type": "string"}}} + _CLOSED_ANSWER: Final = {**_ANSWER, "required": ["text"], "additionalProperties": False} + _LABELS: Final = {"type": "object", "additionalProperties": {"type": "string"}} + + @pytest.mark.parametrize( + ("schema", "expected_schema"), + [ + pytest.param( + {"$ref": "#/$defs/Answer", "$defs": {"Answer": _ANSWER}, "description": "The answer"}, + {"$ref": "#/$defs/Answer", "$defs": {"Answer": _CLOSED_ANSWER}, "description": "The answer"}, + id="root-ref-keeps-defs-and-siblings", + ), + pytest.param( + {"type": "object", "properties": {"labels": _LABELS, "extra": {"type": "object"}}}, + { + "type": "object", + "properties": {"labels": _LABELS, "extra": {"type": "object"}}, + "required": ["labels", "extra"], + "additionalProperties": False, + }, + id="maps-and-free-form-objects-stay-open", + ), + ], + ) + def test_strict_schema_closes_only_objects_with_properties_on_both_adapters( + self, schema: Dict[str, object], expected_schema: Dict[str, object] + ): + request: Final = _make_request(output_format={"type": "json_schema", "schema": schema}) + chat, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(request) + + assert _ADAPTER.translate_request(request)["text"]["format"]["schema"] == expected_schema + assert chat["response_format"]["json_schema"]["schema"] == expected_schema + def test_strict_schema_keeps_properties_named_like_schema_keywords(self): schema: Final = { "type": "object", @@ -2854,10 +2887,25 @@ def test_allowed_callers_answers_400_unless_drop_params_drops_it(allowed_callers assert _translate(True, tools=tools)["tools"] == _ADAPTER.translate_tools_to_responses_api([LOOKUP_TOOL]) -def test_container_answers_400_unless_drop_params_drops_it(): - message: Final = _unsupported_message(container={"id": "container_abc"}) +@pytest.mark.parametrize("container", ["container_abc", {"id": "container_abc"}]) +def test_container_answers_400_unless_drop_params_drops_it(container: str | Dict[str, object]): + message: Final = _unsupported_message(container=container) assert "no equivalent for container. To drop it, set `litellm.drop_params=True`" in message - assert "container" not in _translate(True, container={"id": "container_abc"}) + assert "container" not in _translate(True, container=container) + + +@pytest.mark.parametrize( + "server", + [ + pytest.param("docs", id="string"), + pytest.param(7, id="number"), + pytest.param({"type": "url", "name": "docs"}, id="no-url"), + pytest.param({**DOCS_SERVER, "tool_configuration": "all"}, id="tool-configuration-string"), + ], +) +def test_a_malformed_mcp_server_answers_400_unless_drop_params_drops_it(server: object): + assert "a malformed mcp_servers entry" in _unsupported_message(mcp_servers=[server, DOCS_SERVER]) + assert _translate(True, mcp_servers=[server, DOCS_SERVER])["tools"] == [DOCS_MCP_TOOL] def test_one_400_names_every_unsupported_field_and_how_to_drop_them(): diff --git a/tests/unit/llms/codex/harness/test_transformation.py b/tests/unit/llms/codex/harness/test_transformation.py index 149ab316324..6df322db1ae 100644 --- a/tests/unit/llms/codex/harness/test_transformation.py +++ b/tests/unit/llms/codex/harness/test_transformation.py @@ -5,16 +5,14 @@ codex-cli through a LiteLLM gateway. """ import asyncio -import functools import json from dataclasses import dataclass, field from pathlib import Path from typing import Optional import pytest -from pydantic import BaseModel, JsonValue, ValidationError +from pydantic import BaseModel, ValidationError -from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH from litellm.harness.context import SessionContext from litellm.harness.errors import HarnessError, HarnessInstallFailed, OptionsMismatch from litellm.harness.handlers.cli_handler import CLIHarnessHandler @@ -22,11 +20,11 @@ from litellm.harness.options import ClaudeCodeOptions, CodexOptions from litellm.harness.sandbox.base import CompletedRun from litellm.harness.sandbox.docker import DockerSandbox from litellm.harness.types import Harness, Reasoning, Text, ToolCall, ToolResult -from litellm.llms.base_llm.base_utils import strict_json_schema from litellm.llms.base_llm.harness.transformation import ( HarnessTurnError, HarnessTurnRequest, ) +from litellm.llms.base_llm.harness.utils import strict_json_schema from litellm.llms.codex.harness.transformation import ( CODEX_SCHEMA_FILENAME, CODEX_TOKEN_ENV, @@ -345,61 +343,6 @@ def test_strict_json_schema_recursive(): assert answer["required"] == ["file", "content"] -def test_strict_json_schema_treats_property_names_and_enum_values_as_data(): - schema = strict_json_schema( - { - "type": "object", - "properties": { - "default": {"type": "string"}, - "$ref": {"type": "string", "default": "x"}, - "properties": {"type": "object", "properties": {"x": {"type": "integer"}}}, - "kind": {"enum": [{"type": "object"}], "const": {"type": "object"}, "examples": [{"type": "object"}]}, - "untyped": {"properties": {"z": {"type": "string"}}}, - }, - "$defs": {"default": {"type": "object", "properties": {"y": {"type": "string"}}}}, - "definitions": {"default": {"type": "string"}}, - "patternProperties": {"default": {"type": "string"}}, - } - ) - assert schema == { - "type": "object", - "properties": { - "default": {"type": "string"}, - "$ref": {"type": "string"}, - "properties": { - "type": "object", - "properties": {"x": {"type": "integer"}}, - "required": ["x"], - "additionalProperties": False, - }, - "kind": {"enum": [{"type": "object"}], "const": {"type": "object"}, "examples": [{"type": "object"}]}, - "untyped": {"properties": {"z": {"type": "string"}}, "required": ["z"], "additionalProperties": False}, - }, - "$defs": { - "default": { - "type": "object", - "properties": {"y": {"type": "string"}}, - "required": ["y"], - "additionalProperties": False, - } - }, - "definitions": {"default": {"type": "string"}}, - "patternProperties": {"default": {"type": "string"}}, - "required": ["default", "$ref", "properties", "kind", "untyped"], - "additionalProperties": False, - } - - -def _nested_items(levels: int) -> JsonValue: - return functools.reduce(lambda inner, _: {"items": inner}, range(levels), True) - - -def test_strict_json_schema_refuses_a_schema_nested_past_the_recursion_cap(): - assert strict_json_schema(_nested_items(DEFAULT_MAX_RECURSE_DEPTH)) == _nested_items(DEFAULT_MAX_RECURSE_DEPTH) - with pytest.raises(ValueError, match="nested deeper than"): - strict_json_schema(_nested_items(DEFAULT_MAX_RECURSE_DEPTH + 1)) - - def test_config_overrides_rejects_managed_keys(): for key in ( "model_provider",