refactor(responses): extract shared format mapping between Responses API and Chat Completions bridges - #24417
Conversation
…API and Chat Completions bridges Both bridges (Responses→CC and CC→Responses) independently encoded the same field mapping knowledge. This extracts 4 shared mappings into a single module so future changes only need to happen in one place. Shared mappings: - status ↔ finish_reason bidirectional dicts and functions - response_format ↔ text.format paired conversion functions - provider_specific_fields normalization helper - usage field name translation (input_tokens ↔ prompt_tokens, etc.) No behavioral changes — bridge methods now delegate to the shared module.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR extracts shared bidirectional mapping logic between the Responses API and Chat Completions bridges into a new Key changes:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/responses/format_mapping.py | New shared module with 4 bidirectional mapping utilities. Well-structured with clear inline documentation of asymmetries. response_api_usage_to_chat_usage now correctly makes a defensive copy of dict input. Minor: status_to_finish_reason is defined but has no production call site yet. |
| litellm/completion_extras/litellm_responses_transformation/transformation.py | Removes 6 occurrences of inline provider_specific_fields normalization boilerplate, replaced with normalize_provider_specific_fields(). Dead instance method _map_responses_status_to_finish_reason removed. Behaviorally equivalent after considering the shared helper now returns None (not {}) for unrecognized types. |
| litellm/responses/litellm_completion_transformation/transformation.py | Removes ~130 lines of duplicated usage/format/status mapping logic, replaced with shared helpers. The or chain for normalize_provider_specific_fields(tool) or normalize_provider_specific_fields(function_definition) is safe since the helper returns None (falsy) rather than {}. |
| litellm/responses/utils.py | Thin delegation to response_api_usage_to_chat_usage from the shared module. Behaviorally equivalent. Top-level import added for the shared function. |
| tests/test_litellm/responses/test_format_mapping.py | New test file with comprehensive unit tests covering all 4 mapping functions in both directions, including roundtrip tests. Tests are purely unit tests with no network calls — compliant with the no-real-network-calls policy. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
subgraph Before["Before (duplicated logic)"]
direction TB
A["completion_extras/\nlitellm_responses_transformation/\ntransformation.py\n(Responses→CC)"] -- "own copy of:\n• status→finish_reason\n• PSF normalization\n• text.format→response_format\n• Usage mapping" --> B["responses/\nlitellm_completion_transformation/\ntransformation.py\n(CC→Responses)"]
end
subgraph After["After (shared module)"]
direction TB
C["completion_extras/\nlitellm_responses_transformation/\ntransformation.py"] --> E["responses/format_mapping.py\n\nnormalize_provider_specific_fields()\nstatus_to_finish_reason()\nfinish_reason_to_status()\nresponse_format_to_text_format()\ntext_format_to_response_format()\nresponse_api_usage_to_chat_usage()\nchat_usage_to_response_api_usage()"]
D["responses/\nlitellm_completion_transformation/\ntransformation.py"] --> E
F["responses/utils.py\n(logging)"] --> E
end
Comments Outside Diff (1)
-
tests/test_litellm/responses/test_format_mapping.py, line 1095-1100 (link)Incomplete test assertions in
test_from_dicttest_from_dictonly assertsprompt_tokensbut never validatescompletion_tokensortotal_tokens. A future regression in theoutput_tokens→completion_tokensmapping would pass this test silently.Rule Used: What: Flag any modifications to existing tests and... (source)
Reviews (3): Last reviewed commit: "fix: avoid mutating caller's dict and re..." | Re-trigger Greptile
…ovider_specific_fields
3f3d275
into
BerriAI:litellm_staging_03_23_2026
…mapping refactor(responses): extract shared format mapping between Responses API and Chat Completions bridges
Relevant issues
Fixes #21346
Pre-Submission checklist
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewType
🧹 Refactoring
Changes
Both Responses API bridges (Responses→CC and CC→Responses) independently encoded the same field mapping knowledge across separate files with no cross-reference. This caused:
This PR extracts 4 shared mappings into
litellm/responses/format_mapping.py:normalize_provider_specific_fields()helper