-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Anthropic - working mid-stream fallbacks #13149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
eecbc5e
fix(router.py): add acompletion_streaming_iterator inside router
krrishdholakia 298233c
fix(router.py): working mid-stream fallbacks
krrishdholakia 16bbb38
fix(router.py): more iterations
krrishdholakia 11a313c
fix(router.py): working mid-stream fallbacks with fallbacks set on ro…
krrishdholakia acbc112
fix(router.py): pass prior content back in new request as assistant p…
krrishdholakia 17732df
fix(router.py): add a system prompt to help guide non-prefix supporti…
krrishdholakia a12554d
fix(common_utils.py): support converting `prefix: true` for non-prefi…
krrishdholakia fd13348
fix: reduce LOC in function
krrishdholakia 8cfcdd6
test(test_router.py): add unit tests for new function
krrishdholakia 0a17164
test: add basic unit test
krrishdholakia 2e89d50
fix(router.py): ensure return type of fallback stream is compatible w…
krrishdholakia b08cb13
fix: cleanup
krrishdholakia a142624
test: update test
krrishdholakia aadbbd5
fix: fix linting error
krrishdholakia 6c90dd4
Merge branch 'main' into litellm_dev_07_30_2025_p1_v3
krrishdholakia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -940,8 +940,8 @@ def _optional_combine_thinking_block_in_choices( | |
| and not self.sent_last_thinking_block | ||
| and model_response.choices[0].delta.content | ||
| ): | ||
| model_response.choices[0].delta.content = ( | ||
| "</think>" + (model_response.choices[0].delta.content or "") | ||
| model_response.choices[0].delta.content = "</think>" + ( | ||
| model_response.choices[0].delta.content or "" | ||
| ) | ||
| self.sent_last_thinking_block = True | ||
|
|
||
|
|
@@ -1841,13 +1841,25 @@ async def __anext__(self): # noqa: PLR0915 | |
| self.logging_obj.async_failure_handler(e, traceback_exception) # type: ignore | ||
| ) | ||
| ## Map to OpenAI Exception | ||
| raise exception_type( | ||
| model=self.model, | ||
| custom_llm_provider=self.custom_llm_provider, | ||
| original_exception=e, | ||
| completion_kwargs={}, | ||
| extra_kwargs={}, | ||
| ) | ||
| try: | ||
| exception_type( | ||
| model=self.model, | ||
| custom_llm_provider=self.custom_llm_provider, | ||
| original_exception=e, | ||
| completion_kwargs={}, | ||
| extra_kwargs={}, | ||
| ) | ||
| except Exception as e: | ||
| from litellm.exceptions import MidStreamFallbackError | ||
|
|
||
| raise MidStreamFallbackError( | ||
| message=str(e), | ||
| model=self.model, | ||
| llm_provider=self.custom_llm_provider or "anthropic", | ||
| original_exception=e, | ||
| generated_content=self.response_uptil_now, | ||
| is_pre_first_chunk=not self.sent_first_chunk, | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Exception Handling Refactor Causes ErrorsThe refactored exception handling introduces two issues:
|
||
|
|
||
| @staticmethod | ||
| def _strip_sse_data_from_chunk(chunk: Optional[str]) -> Optional[str]: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Exception Handling Fails to Map Errors
The exception handling logic is incorrect. The
exception_type()call, which previously raised a mapped exception, is now merely called within atryblock without being raised. This prevents the intended exception mapping and can mask the original error. Additionally, the innerexcept Exception as e:shadows the original exception variable, causingMidStreamFallbackErrorto be instantiated with the wrongoriginal_exceptionandmessage(referring to the inner exception instead of the original one).Locations (1)
litellm/litellm_core_utils/streaming_handler.py#L1843-L1862