Skip to content

fix(proxy): strip every TypedDict qualifier before numeric form-field detection - #39780

Merged
mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_/goofy-bohr-6cd011
Sep 5, 2026
Merged

mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_/goofy-bohr-6cd011

Conversation

@yuneng-berri

@yuneng-berri yuneng-berri commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • A numeric form field wrapped in Required/NotRequired was silently dropped
  • Python 3.10 keeps those wrappers, so the mapped test fails on that leg only
  • The house convention pushes schema fields toward exactly that shape

How it solves it:

  • Peel every qualifier layer, in any order, instead of one ReadOnly
  • Behaviour is identical on 3.10 through 3.14 for the shipped schema
  • Test now covers reverse nesting order and include_extras=True

User Flow

Before: every PR opened against litellm_internal_staging shows the proxy-infra / Run tests (Python 3.10) check red on a test its branch never touched, and the staging branch's own run is red the same way

  1. A contributor pushes a branch and opens a PR against litellm_internal_staging at https://github.com/BerriAI/litellm/compare
  2. They open the Checks tab at https://github.com/BerriAI/litellm/pull/{number}/checks and see proxy-infra / Run tests (Python 3.10) fail while the 3.11 through 3.14 legs of the same shard pass
  3. They open the failed job's log and find tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py::TestNumericFormFields::test_qualifiers_and_optionality_are_unwrapped failing with assert {'read_only'} == {'read_only', 'not_required', 'required', ...}, in a file their branch never changed
  4. They re-run the job and it fails the same way, and the staging branch's latest push run at https://github.com/BerriAI/litellm/actions/workflows/test-unit.yml carries the same red

After: the same PR's proxy-infra / Run tests (Python 3.10) check passes alongside the other legs, and the staging branch's own run is green

  1. A contributor pushes a branch and opens a PR against litellm_internal_staging at https://github.com/BerriAI/litellm/compare
  2. They open the Checks tab at https://github.com/BerriAI/litellm/pull/{number}/checks and see proxy-infra / Run tests (Python 3.10) green with the 3.11 through 3.14 legs
  3. There is no failed job to open: the check list reflects only their own change
  4. The staging branch's latest push run at https://github.com/BerriAI/litellm/actions/workflows/test-unit.yml is green too

Relevant issues

Linear ticket

Resolves LIT-6947

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

The check this PR exists for

  1. Before, the staging branch's own Unit Tests run at 59d42d36e6: proxy-infra / Run tests (Python 3.10) fails on TestNumericFormFields::test_qualifiers_and_optionality_are_unwrapped while the 3.11 through 3.14 legs of the same shard pass: https://github.com/BerriAI/litellm/actions/runs/33933426456/job/101216565174
  2. After, the same shard at this PR's tip cd113c3a2e: all five legs pass, the 3.10 one included: https://github.com/BerriAI/litellm/actions/runs/33935529515/job/101222597269

What the peel changes for a gateway user

Both legs run on Python 3.10 (the only leg that reproduces this) with a proxy booted with --num_workers 2 on a random free port against real providers, and differ only in the commit the proxy was booted from: the Before leg is the merge base 59d42d36e6, the After leg is this PR's tip cd113c3a2e. Shared setup:

# config.yaml
model_list:
  - model_name: nova-canvas
    litellm_params:
      model: bedrock/amazon.nova-canvas-v1:0
      aws_region_name: us-east-1
  - model_name: gemini-image
    litellm_params:
      model: gemini/gemini-3.1-flash-image
      api_key: os.environ/GEMINI_API_KEY

general_settings:
  master_key: sk-1234
python3.10 litellm/proxy/proxy_cli.py --config config.yaml --port 27831 --num_workers 2 --detailed_debug

square1024.png is a 1024x1024 PNG. Responses are summarised as their status code plus how many images came back, since each b64_json is over a megabyte

The shipped ImageEditRequestParams writes n as a bare int | None, which never trips the bug, so case 2 below writes that one field the way the repo's TypedDict convention asks for (n: NotRequired[ReadOnly[int | None]]) to show what the fix buys. That schema line is a local QA change only, applied identically on both legs, and is not part of this PR

Before (59d42d3)

Case 1: shipped schema, n=2 sent as a form field

  1. curl -s -w '\nHTTP %{http_code}' -X POST http://127.0.0.1:27831/v1/images/edits \
      -H "Authorization: Bearer sk-1234" \
      -F "model=nova-canvas" -F "image=@square1024.png" \
      -F "prompt=make the square golden" -F "n=2"
  2. HTTP 200
    images: 2
      [0] b64_json length 2092780
      [1] b64_json length 2275496
    
  3. Same call with -F "model=gemini-image" and -F "n=1":
    HTTP 200
    images: 1
      [0] b64_json length 763732
    

Case 2: n written the house-convention way

  1. Same Nova Canvas call as case 1, n=2
  2. HTTP 400
    {
      "error": {
        "message": "litellm.BadRequestError: BedrockException - {\"message\":\"Malformed input request: #/imageGenerationConfig/numberOfImages: expected type: Number, found: String, please reformat your input and try again.\"}. Received Model Group=nova-canvas\nAvailable Model Group Fallbacks=None",
        "type": "invalid_request_error",
        "param": null,
        "code": "400"
      }
    }
    
  3. Same Gemini call as case 1, n=1: still HTTP 200, images: 1 (Gemini accepts a text count, so the symptom never shows there)

After (cd113c3)

Case 1: shipped schema, n=2 sent as a form field

  1. Same Nova Canvas call as before
  2. HTTP 200
    images: 2
      [0] b64_json length 2092780
      [1] b64_json length 2275496
    
  3. Same Gemini call as before:
    HTTP 200
    images: 1
      [0] b64_json length 697272
    

Case 2: n written the house-convention way

  1. Same Nova Canvas call as before, n=2
  2. HTTP 200
    images: 2
      [0] b64_json length 2092780
      [1] b64_json length 2275496
    
  3. Same Gemini call as before, n=1: HTTP 200, images: 1

Observations from the run:

  • Gemini tolerates a text count, so only Bedrock shows the symptom
  • Shipped schema behaves the same on both legs; fix is latent today

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Latent for the shipped schema: n is a bare int | None, so today's gateway forwards it as a number on every Python already; the peel guards the NotRequired[ReadOnly[...]] shape the TypedDict convention produces. Rewriting the shipped schema here would be scope creep on a CI fix, so it stays
  • The 3.10 unit-test legs are not among the branch ruleset's required contexts (only the 3.12 <shard> / Run tests legs are), so the red blocked PRs by being visible rather than through the merge gate. Left alone: which legs gate merges is a ruleset decision, not this PR's
  • _unqualified is allowlisted in tests/code_coverage_tests/recursive_detector.py like the sibling _flatten_form_field: its recursion is bounded by the qualifier depth of a static annotation, and a depth cap would only add a branch no schema can reach
  • CircleCI proxy_store_model_in_db_tests is red at this tip on test_chat_completion_bad_model_with_spend_logs (assert '' == 'non-existent-model'), the same staging-wide failure the branch shows on its own; test(store_model_in_db): assert the 400 contract in the unknown-model spend log test #39842 fixes that test under LIT-6949, and the job is not a ruleset-required context

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

  • cd113c3 passes /live-pr-risk

… detection

_numeric_form_type only peeled a single ReadOnly layer, so a field still
wrapped in Required/NotRequired was read as non-numeric and dropped from the
mapping. Which qualifiers survive get_type_hints varies by interpreter version
and by include_extras, so on Python 3.10 NotRequired[ReadOnly[int]] reached the
check intact and the field was silently skipped, which is what turns the mapped
test red on the 3.10 leg only.

Peel Required/NotRequired/ReadOnly/Annotated in any order and nesting instead.
The one production caller feeds a schema with no qualifiers, so the resulting
mapping is unchanged on every interpreter in the matrix, but a field written the
house-convention way stops being dropped.
@greptile-apps

greptile-apps Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR makes numeric multipart form-field detection recursively remove supported TypedDict and annotation qualifiers before identifying int and float fields.

  • Adds recursive handling for Annotated, Required, NotRequired, and ReadOnly.
  • Adds coverage for reversed qualifier nesting and get_type_hints(..., include_extras=True).
  • Exempts the structurally bounded annotation helper from the recursive-function detector.

Confidence Score: 5/5

The PR appears safe to merge with no actionable failures established in the current request path.

The recursive unwrapping preserves existing scalar and union handling, and the expanded tests cover the qualifier combinations introduced by this fix.

Important Files Changed

Filename Overview
litellm/proxy/common_utils/http_parsing_utils.py Recursively unwraps recognized annotation qualifiers before numeric form-field type detection.
tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py Extends numeric-field tests across nested qualifier orders and preserved annotation extras.
tests/code_coverage_tests/recursive_detector.py Documents and exempts the bounded qualifier recursion from generic recursion checks.

Reviews (2): Last reviewed commit: "ci: allowlist the bounded _unqualified q..." | Re-trigger Greptile

@codspeed

codspeed Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_/goofy-bohr-6cd011 (cd113c3) with litellm_internal_staging (59d42d3)

Open in CodSpeed

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri
mateo-berri requested a review from a team September 5, 2026 01:13
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit cd113c3. Configure here.

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants