Skip to content

fix(bedrock): guard empty choices in mistral get_outputText#34610

Open
dfedoryshchev wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
dfedoryshchev:fix-bedrock-mistral-empty-choices
Open

fix(bedrock): guard empty choices in mistral get_outputText#34610
dfedoryshchev wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
dfedoryshchev:fix-bedrock-mistral-empty-choices

Conversation

@dfedoryshchev

Copy link
Copy Markdown

AmazonMistralConfig.get_outputText guards choices by key presence only, so a present-but-empty choices ({"choices": []}, which a bedrock mistral/pixtral response can carry for a filtered or empty generation) reaches completion_response["choices"][0] and raises an uncaught IndexError that surfaces as a 500.

Added a length check so an empty choices falls through to the existing else and returns a clean BedrockError(status_code=400) instead. This mirrors the guard the sibling OpenAI-format handler already uses in invoke_handler.py ("choices" in completion_response and len(...) > 0).

Regression test feeds {"choices": []} through get_outputText and asserts BedrockError; it raised IndexError before this change.

@CLAassistant

CLAassistant commented Jul 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR prevents empty Bedrock Mistral choices arrays from causing an indexing failure

  • Requires choices to contain an item before parsing the OpenAI-compatible response shape
  • Falls through to the existing BedrockError path for an empty response
  • Adds a focused regression test for {"choices": []}

Confidence Score: 5/5

The PR appears safe to merge

The updated condition prevents indexing an empty choices array, preserves handling of nonempty choices and outputs responses, and the regression test directly exercises the corrected failure case

Important Files Changed

Filename Overview
litellm/llms/bedrock/chat/invoke_transformations/amazon_mistral_transformation.py Safely guards access to the first Mistral choice while preserving the existing outputs fallback and error behavior
tests/test_litellm/llms/bedrock/chat/test_mistral_config.py Adds a focused regression test proving that an empty choices array raises the expected BedrockError

Reviews (1): Last reviewed commit: "fix(bedrock): guard empty choices in mis..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing dfedoryshchev:fix-bedrock-mistral-empty-choices (c586769) with litellm_internal_staging (b9b27c2)

Open in CodSpeed

if "choices" in completion_response and len(completion_response["choices"]) > 0:
outputText = completion_response["choices"][0]["message"]["content"]
model_response.choices[0].finish_reason = completion_response["choices"][0]["finish_reason"]
elif "outputs" in completion_response:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

the outputs branch a few lines down indexes [0] the same way, so {"outputs": []} still raises IndexError instead of BedrockError. worth guarding both while you are here.

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.

3 participants