Skip to content

fix(streaming): preserve provider status for string error codes - #40645

Open
wzj1228516103 wants to merge 2 commits into
BerriAI:mainfrom
wzj1228516103:litellm_issue_40566
Open

wzj1228516103 wants to merge 2 commits into
BerriAI:mainfrom
wzj1228516103:litellm_issue_40566

Conversation

@wzj1228516103

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Groq uses tool_use_failed as a string error code.
  • Streaming fallback currently casts it directly to int.
  • This raises ValueError and hides the provider error.

How it solves it:

  • Safely parse numeric provider status codes.
  • Fall back to the original HTTP response status.
  • Preserve the original provider error message.

User Flow

Before: a Groq streaming tool call fails with an internal conversion error.

  1. A client sends a streaming tool call to the Groq-compatible chat completions endpoint.
  2. Groq returns an HTTP 400 response with the provider code tool_use_failed.
  3. LiteLLM attempts to convert that string to an integer.
  4. The request surfaces a ValueError instead of the provider's tool schema error.

After: the same request exposes the provider's HTTP status and error message.

  1. A client sends the same streaming tool call to the Groq-compatible chat completions endpoint.
  2. Groq returns an HTTP 400 response with the provider code tool_use_failed.
  3. LiteLLM ignores the non-numeric provider code for status conversion.
  4. The request preserves HTTP status 400 and the original tool validation error.

Relevant issues

Fixes #40566

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • 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

Screenshots / Proof of Fix

Before (merge base)

  1. A regression test using status_code = "tool_use_failed" fails during MidStreamFallbackError construction with ValueError.

After (57debdb)

  1. The regression test constructs the fallback error successfully.
  2. The resulting status code is 400.
  3. The original provider error message is preserved.
  4. git diff --check and Python compilation checks pass.

Note: the focused pytest command could not run in this environment because the LiteLLM editable build requires the Rust MSVC linker (link.exe), which is unavailable.

Type

🐛 Bug Fix
✅ Test

Caveats

Low

  • Full pytest verification remains pending in CI.
  • No live Groq credentials were used locally.

Final Attestation

  • The tests check the string provider-code regression case.
  • Full CI verification is pending.

@wzj1228516103
wzj1228516103 requested a review from a team September 11, 2026 01:21
@greptile-apps

greptile-apps Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR prevents nonnumeric provider error codes from masking streaming failures and recovers the HTTP status from the associated response

  • Adds safe numeric status parsing for mid-stream fallback errors
  • Preserves the provider response status when the provider error code is nonnumeric
  • Adds focused regression coverage for the string-code case

Confidence Score: 4/5

The behavior change appears correct, but the redundant test docstring must be removed to satisfy the repository’s explicit comment policy

The status fallback preserves the provider’s HTTP status and message without a demonstrated behavioral regression; only a repository-guideline violation remains

Files Needing Attention: tests/test_litellm/test_exception_header_preservation.py

Important Files Changed

Filename Overview
litellm/exceptions.py Safely parses provider status values and falls back to the associated HTTP response status
tests/test_litellm/test_exception_header_preservation.py Adds focused regression coverage, but includes a redundant docstring contrary to repository guidance

Reviews (1): Last reviewed commit: "fix(streaming): preserve provider status..." | Re-trigger Greptile

assert str(midstream_fallback.response.request.url) == "https://openai.com/v1/"

def test_midstream_fallback_error_accepts_non_numeric_provider_status(self):
"""Provider error codes can be strings while the response still has an HTTP status."""

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.

P2 Redundant test docstring

This docstring restates the test name, violating the repository requirement that comments only explain genuinely complex logic. Remove it before merging

Suggested change
"""Provider error codes can be strings while the response still has an HTTP status."""

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@codspeed

codspeed Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing wzj1228516103:litellm_issue_40566 (ab2de41) with litellm_internal_staging (dca71e2)

Open in CodSpeed

@codecov

codecov Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:52
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 14:36
@mateo-berri

Copy link
Copy Markdown
Contributor

#41528 just merged and edits the same MidStreamFallbackError constructor, so this branch now conflicts. Could you merge main in before review?

This branch has not been deployed

No deployments
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.

[Bug]: Groqs failed tool schema error not correctly fed back

3 participants