Skip to content

fix: simplify identical source/target loop in payload builder - #356

Open
andrewwhitecdw wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
andrewwhitecdw:codequality/client-simplify-identical-source-target-loop
Open

fix: simplify identical source/target loop in payload builder#356
andrewwhitecdw wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
andrewwhitecdw:codequality/client-simplify-identical-source-target-loop

Conversation

@andrewwhitecdw

@andrewwhitecdw andrewwhitecdw commented Aug 11, 2026

Copy link
Copy Markdown

fix: simplify identical source/target loop in payload builder

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.

-    for source, target in (("temperature", "temperature"), ("top_p", "top_p")):
-        value = sampling.get(source)
-        if value is not None:
-            payload[target] = value
+    for key in ("temperature", "top_p"):
+        value = sampling.get(key)
+        if value is not None:
+            payload[key] = value

Testing

  • pytest tests/test_client.py tests/test_payload_sampling.py -v: 34 passed
  • Full pytest has one pre-existing failure unrelated to this change: test_stage_router_drives_both_litellm_targets.
  • mypy 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 the payload builder.

Contributor guidelines

  • DCO sign-off: included.
  • Squash: one commit.

Summary by CodeRabbit

  • Bug Fixes

    • Ensured temperature and top-p sampling settings are correctly forwarded in generated requests.
  • Tests

    • Added coverage verifying that both sampling values are preserved in request payloads.

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 <andrewwhitecdw@users.noreply.github.com>
@andrewwhitecdw
andrewwhitecdw requested a review from a team as a code owner August 11, 2026 15:24
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The LiteLLM payload mapping now iterates directly over temperature and top_p. A test verifies that both sampling values remain in the generated payload.

Changes

LiteLLM sampling payload

Layer / File(s) Summary
Sampling payload mapping and validation
examples/experimental/litellm/src/switchyard_litellm/client.py, examples/experimental/litellm/tests/test_payload_sampling.py
The mapping loop directly processes temperature and top_p without changing conditional inclusion. The test verifies that both values are preserved.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Poem

I’m a rabbit with a payload to send,
Temperature and top-p now follow the trend.
The loop hops lightly, neat and bright,
Tests guard each value just right.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: simplifying the identical source-to-target loop in the payload builder.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
examples/experimental/litellm/tests/test_payload_sampling.py (1)

4-4: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a docstring to the test entry point.

test_sampling_temperature_and_top_p_forwarded is a public pytest function without a docstring. Add a concise docstring that states it verifies forwarding of sampling.temperature and sampling.top_p.

As per coding guidelines: “Add concise triple-quoted docstrings for public functions, classes, methods, and API entry points.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/experimental/litellm/tests/test_payload_sampling.py` at line 4, Add
a concise triple-quoted docstring to the
test_sampling_temperature_and_top_p_forwarded pytest function stating that it
verifies forwarding of sampling.temperature and sampling.top_p, without changing
the test behavior.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@examples/experimental/litellm/tests/test_payload_sampling.py`:
- Line 4: Add a concise triple-quoted docstring to the
test_sampling_temperature_and_top_p_forwarded pytest function stating that it
verifies forwarding of sampling.temperature and sampling.top_p, without changing
the test behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 28e61915-d56e-4c61-97a7-0987c95c3d1d

📥 Commits

Reviewing files that changed from the base of the PR and between fb3fc30 and a234d2a.

📒 Files selected for processing (2)
  • examples/experimental/litellm/src/switchyard_litellm/client.py
  • examples/experimental/litellm/tests/test_payload_sampling.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant