style(responses): build text.format without mutable-collection literals

litellm main tightened the LIT002 (mutable-collection construction)
ceiling after this branch was cut and is currently over it, so the
type-discipline gate now rejects any net-new dict literal. Build the two
ResponseText values as TypedDict-annotated literals and freeze the
intermediate mappings with MappingProxyType -- the forms the checker
documents as one-shot builds rather than seed-then-mutate accumulators.

No behaviour change; the tests are untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
MHammett 2026-09-21 04:33:19 -05:00
parent c5d0800004
commit c7daa77fc2

View file

@ -940,12 +940,13 @@ class ResponsesAPIRequestUtils:
remove. A bare ValueError would surface through exception_type() as
APIConnectionError, reporting malformed input as a network fault.
"""
from types import MappingProxyType
from litellm.llms.base_llm.base_utils import type_to_response_format_param
# Normalizes a Pydantic model into a response_format dict; passes a dict through.
converted: Final = type_to_response_format_param(response_format) or {}
format_type: Final = converted.get("type")
if format_type is None:
converted: Final = type_to_response_format_param(response_format)
if converted is None or converted.get("type") is None:
raise litellm.BadRequestError(
message=(
f"Could not read a `type` from the supplied response format: {response_format!r}. "
@ -955,18 +956,23 @@ class ResponsesAPIRequestUtils:
model=None,
llm_provider=None,
)
format_type: Final = converted["type"]
if format_type != "json_schema":
return {"format": {"type": format_type}}
schema_less: Final[ResponseText] = {"format": {"type": format_type}}
return schema_less
json_schema: Final = converted.get("json_schema") or {}
json_schema: Final = converted.get("json_schema") or MappingProxyType({})
# Only `strict`/`description` are optional; a missing `name`/`schema` is the
# provider's error to report, not ours to guess at.
schema_fields: Final = {
key: json_schema[key]
for key in ("name", "schema", "strict", "description")
if json_schema.get(key) is not None
}
return {"format": {"type": format_type, **schema_fields}}
schema_fields: Final = MappingProxyType(
{
key: json_schema[key]
for key in ("name", "schema", "strict", "description")
if json_schema.get(key) is not None
}
)
text_param: Final[ResponseText] = {"format": {"type": format_type, **schema_fields}}
return text_param
@staticmethod
def convert_text_format_to_text_param(