From a234d2a4ecf82b73dd7d2c8cd1ee8d6bacce94be Mon Sep 17 00:00:00 2001 From: andrewwhitecdw Date: Tue, 11 Aug 2026 10:15:38 -0500 Subject: [PATCH] fix(client): simplify identical source/target loop in payload builder Summary Replace the explicit source/target tuple loop where source and target are always the same key with a single-key loop, improving readability without changing behavior. Root cause The loop copied sampling values with a source-to-target mapping, but the keys ('temperature', 'top_p') were identical on both sides, so the mapping added noise without adding flexibility. Fix Iterate over the keys directly. Behavior is preserved: values are still copied only when present. Testing - pytest tests/test_client.py tests/test_payload_sampling.py -v: 34 passed - Full pytest examples/experimental/litellm/tests/ has one pre-existing failure unrelated to this change: test_stage_router_drives_both_litellm_targets (KeyError: 'selected_model'). - mypy examples/experimental/litellm/src/switchyard_litellm/client.py reports one pre-existing [no-redef] error on line 127 (also present on unmodified main). Why existing tests missed it No functional change; the new regression test explicitly verifies temperature/top_p are forwarded through _payload. Contributor guidelines - DCO sign-off: included (Signed-off-by). - Squash: one commit. Fixes #2094 Signed-off-by: andrewwhitecdw --- .../litellm/src/switchyard_litellm/client.py | 6 +++--- .../litellm/tests/test_payload_sampling.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 examples/experimental/litellm/tests/test_payload_sampling.py diff --git a/examples/experimental/litellm/src/switchyard_litellm/client.py b/examples/experimental/litellm/src/switchyard_litellm/client.py index dc0286c1f..7ee069948 100644 --- a/examples/experimental/litellm/src/switchyard_litellm/client.py +++ b/examples/experimental/litellm/src/switchyard_litellm/client.py @@ -236,10 +236,10 @@ def _payload(request: Mapping[str, object], model: str) -> dict[str, Any]: tool_choice = _tool_choice(request) if tool_choice is not None: payload["tool_choice"] = tool_choice - for source, target in (("temperature", "temperature"), ("top_p", "top_p")): - value = sampling.get(source) + for key in ("temperature", "top_p"): + value = sampling.get(key) if value is not None: - payload[target] = value + payload[key] = value max_tokens = output.get("max_output_tokens") if max_tokens is not None: payload["max_completion_tokens"] = max_tokens diff --git a/examples/experimental/litellm/tests/test_payload_sampling.py b/examples/experimental/litellm/tests/test_payload_sampling.py new file mode 100644 index 000000000..edd23f68f --- /dev/null +++ b/examples/experimental/litellm/tests/test_payload_sampling.py @@ -0,0 +1,11 @@ +from switchyard_litellm.client import _payload + + +def test_sampling_temperature_and_top_p_forwarded() -> None: + request = { + "messages": [{"role": "user", "content": [{"type": "text", "text": "hi"}]}], + "sampling": {"temperature": 0.5, "top_p": 0.9}, + } + payload = _payload(request, "strong") + assert payload["temperature"] == 0.5 + assert payload["top_p"] == 0.9