mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(responses): make _as_input_param iterative, preserve tuple/list identity, and drop cast import
This commit is contained in:
parent
98fa0dc010
commit
67afc09324
3 changed files with 72 additions and 37 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from collections.abc import Callable, Collection, Iterable, Mapping, Sequence
|
||||
from collections.abc import Callable, Collection, Mapping, Sequence
|
||||
from functools import lru_cache
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Any, Final, Protocol, cast, get_type_hints
|
||||
|
|
@ -749,23 +749,7 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig):
|
|||
|
||||
@staticmethod
|
||||
def _materialize_input_value(value: object) -> object:
|
||||
if isinstance(value, BaseModel):
|
||||
return value.model_dump(exclude_none=True)
|
||||
if isinstance(value, dict):
|
||||
materialized_dict: Final = {
|
||||
key: OpenAIResponsesAPIConfig._materialize_input_value(val) for key, val in value.items()
|
||||
}
|
||||
return value if all(materialized_dict[key] is value[key] for key in value) else materialized_dict
|
||||
if isinstance(value, list):
|
||||
materialized_list: Final = [OpenAIResponsesAPIConfig._materialize_input_value(part) for part in value]
|
||||
return (
|
||||
value
|
||||
if all(new is old for new, old in zip(materialized_list, value, strict=True))
|
||||
else materialized_list
|
||||
)
|
||||
if isinstance(value, Iterable) and not isinstance(value, (str, bytes, bytearray, Mapping)):
|
||||
return [OpenAIResponsesAPIConfig._materialize_input_value(part) for part in value]
|
||||
return value
|
||||
return _as_input_param(value)
|
||||
|
||||
def _validate_input_param(self, input: str | ResponseInputParam) -> str | ResponseInputParam:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
import fnmatch
|
||||
import os
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from typing import TYPE_CHECKING, Final, Literal, Optional, cast
|
||||
from typing import TYPE_CHECKING, Final, Literal, Optional
|
||||
|
||||
import httpx
|
||||
from pydantic import JsonValue, TypeAdapter, ValidationError
|
||||
|
|
@ -32,6 +32,7 @@ from litellm.types.proxy.guardrails.guardrail_hooks.generic_guardrail_api import
|
|||
GenericGuardrailAPIMetadata,
|
||||
GenericGuardrailAPIRequest,
|
||||
GenericGuardrailAPIResponse,
|
||||
structured_messages_from_response,
|
||||
GuardrailToolParam,
|
||||
)
|
||||
from litellm.types.utils import GenericGuardrailAPIInputs
|
||||
|
|
@ -591,12 +592,12 @@ class GenericGuardrailAPI(CustomGuardrail):
|
|||
|
||||
headers: Final = self._build_request_headers()
|
||||
request_payload: Final = self._request_body(guardrail_request, structured_messages)
|
||||
shown_messages_raw: Final = request_payload.get("structured_messages")
|
||||
shown_from_payload: Final = structured_messages_from_response(
|
||||
request_payload.get("structured_messages")
|
||||
)
|
||||
shown_messages: Final = (
|
||||
cast( # cast-ok: JSON-serialized structured_messages from GenericGuardrailAPIRequest
|
||||
Sequence[AllMessageValues], shown_messages_raw
|
||||
)
|
||||
if isinstance(shown_messages_raw, list)
|
||||
shown_from_payload
|
||||
if shown_from_payload is not None
|
||||
else guardrail_request.structured_messages
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -473,25 +473,75 @@ def without_item_cache_control(input: str | ResponseInputParam) -> str | Respons
|
|||
)
|
||||
|
||||
|
||||
def _expand_param_node(
|
||||
node: object,
|
||||
) -> tuple[str, object, object, tuple[object, ...]] | None:
|
||||
if isinstance(node, BaseModel):
|
||||
return ("forward", node, 1, (node.model_dump(exclude_none=True),))
|
||||
if isinstance(node, dict):
|
||||
keys: Final = tuple(node.keys())
|
||||
return ("dict", node, keys, tuple(node[k] for k in keys))
|
||||
if isinstance(node, (list, tuple)):
|
||||
kind: Final = "list" if isinstance(node, list) else "tuple"
|
||||
return (kind, node, len(node), tuple(node))
|
||||
if isinstance(node, Iterable) and not isinstance(node, (str, bytes, bytearray, Mapping)):
|
||||
items: Final = tuple(node)
|
||||
return ("iter", items, len(items), items)
|
||||
return None
|
||||
|
||||
|
||||
def _rebuild_param_container(
|
||||
op: str,
|
||||
node: object,
|
||||
extra: object,
|
||||
children: Sequence[object],
|
||||
) -> object:
|
||||
if op == "forward":
|
||||
return children[0]
|
||||
if op == "dict" and isinstance(node, dict) and isinstance(extra, tuple):
|
||||
rebuilt: Final = dict(zip(extra, children, strict=True))
|
||||
return node if all(rebuilt[k] is node[k] for k in extra) else rebuilt
|
||||
if op == "list" and isinstance(node, list):
|
||||
return node if all(new is old for new, old in zip(children, node, strict=True)) else list(children)
|
||||
if op == "tuple" and isinstance(node, tuple):
|
||||
return node if all(new is old for new, old in zip(children, node, strict=True)) else tuple(children)
|
||||
return list(children)
|
||||
|
||||
|
||||
def _as_input_param(item: object) -> object:
|
||||
if isinstance(item, BaseModel):
|
||||
return item.model_dump(exclude_none=True)
|
||||
if isinstance(item, dict):
|
||||
materialized_dict: Final = {key: _as_input_param(value) for key, value in item.items()}
|
||||
return item if all(materialized_dict[key] is item[key] for key in item) else materialized_dict
|
||||
if isinstance(item, list):
|
||||
materialized_list: Final = [_as_input_param(part) for part in item]
|
||||
return item if all(new is old for new, old in zip(materialized_list, item, strict=True)) else materialized_list
|
||||
if isinstance(item, Iterable) and not isinstance(item, (str, bytes, bytearray, Mapping)):
|
||||
return [_as_input_param(part) for part in item]
|
||||
return item
|
||||
work: Final[list[tuple[str, object, object]]] = [ # mutable-ok: iterative post-order stack, recursion is banned
|
||||
("visit", item, None)
|
||||
]
|
||||
out: Final[list[object]] = [] # mutable-ok: post-order value stack
|
||||
while work:
|
||||
op, current, extra = work.pop()
|
||||
if op == "visit":
|
||||
expanded = _expand_param_node(current)
|
||||
if expanded is None:
|
||||
out.append(current)
|
||||
else:
|
||||
kind, target, meta, children = expanded
|
||||
work.append((kind, target, meta))
|
||||
for child in reversed(children):
|
||||
work.append(("visit", child, None))
|
||||
else:
|
||||
arity = len(extra) if isinstance(extra, tuple) else extra
|
||||
assert isinstance(arity, int)
|
||||
built_children = out[-arity:] if arity else []
|
||||
if arity:
|
||||
del out[-arity:]
|
||||
out.append(_rebuild_param_container(op, current, extra, built_children))
|
||||
return out[0]
|
||||
|
||||
|
||||
def with_compaction_items_as_params(input: str | ResponseInputParam) -> str | ResponseInputParam:
|
||||
if not isinstance(input, list):
|
||||
return input
|
||||
return cast( # cast-ok: the same input items, a compaction item replayed from SDK output dumped to its param dict
|
||||
ResponseInputParam, [_as_input_param(item) for item in input]
|
||||
materialized: Final = [_as_input_param(item) for item in input]
|
||||
if all(new is old for new, old in zip(materialized, input, strict=True)):
|
||||
return input
|
||||
return cast( # cast-ok: the same input items, SDK output models and single-pass iterables dumped to param dicts
|
||||
ResponseInputParam, materialized
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue