-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(mistralai): standardize API error handling #7308
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
tinalenguyen
merged 1 commit into
livekit:main
from
jeanprbt:jeanprbt/standardize-mistral-errors
Sep 17, 2026
+189
−13
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import types | ||
| from collections.abc import Callable | ||
|
|
||
| import httpx | ||
| import pytest | ||
| from mistralai.client.errors import HTTPValidationError, HTTPValidationErrorData, SDKError | ||
| from mistralai.client.models import MessageOutputEvent | ||
|
|
||
| from livekit.agents import APIStatusError, llm | ||
| from livekit.agents.types import APIConnectOptions | ||
| from livekit.plugins.mistralai.llm import LLM | ||
|
|
||
| pytestmark = pytest.mark.plugin("mistralai") | ||
|
|
||
|
|
||
| def _event(data: object) -> object: | ||
| return types.SimpleNamespace(data=data) | ||
|
|
||
|
|
||
| class _FakeConversations: | ||
| def __init__(self, handler: Callable[[int], object]) -> None: | ||
| self._handler = handler | ||
| self.calls = 0 | ||
|
|
||
| async def start_stream_async(self, **kwargs: object) -> object: | ||
| self.calls += 1 | ||
| result = self._handler(self.calls) | ||
| if isinstance(result, BaseException): | ||
| raise result | ||
| return result | ||
|
|
||
|
|
||
| def _model(conversations: _FakeConversations) -> LLM: | ||
| client = types.SimpleNamespace( | ||
| beta=types.SimpleNamespace(conversations=conversations), | ||
| ) | ||
| return LLM(client=client, model="test-model") # type: ignore[arg-type] | ||
|
|
||
|
|
||
| def _sdk_error(status_code: int) -> SDKError: | ||
| body = '{"message":"provider error"}' | ||
| response = httpx.Response( | ||
| status_code, | ||
| headers={"content-type": "application/json", "x-request-id": "req_test"}, | ||
| content=body, | ||
| request=httpx.Request("POST", "https://api.mistral.ai/v1/conversations"), | ||
| ) | ||
| return SDKError("provider error", response, body) | ||
|
|
||
|
|
||
| def _validation_error() -> HTTPValidationError: | ||
| body = '{"detail":[{"msg":"invalid request"}]}' | ||
| response = httpx.Response( | ||
| 422, | ||
| headers={"content-type": "application/json", "x-request-id": "req_validation"}, | ||
| content=body, | ||
| request=httpx.Request("POST", "https://api.mistral.ai/v1/conversations"), | ||
| ) | ||
| return HTTPValidationError(HTTPValidationErrorData(), response, body) | ||
|
|
||
|
|
||
| class TestLLMErrorHandling: | ||
| @pytest.mark.asyncio | ||
| async def test_client_error_is_not_retried(self) -> None: | ||
| conversations = _FakeConversations(lambda _: _sdk_error(400)) | ||
| model = _model(conversations) | ||
| errors: list[llm.LLMError] = [] | ||
| model.on("error", errors.append) | ||
|
|
||
| try: | ||
| with pytest.raises(APIStatusError) as excinfo: | ||
| async with model.chat( | ||
| chat_ctx=llm.ChatContext.empty(), | ||
| conn_options=APIConnectOptions(max_retry=3, retry_interval=0.0, timeout=1.0), | ||
| ) as stream: | ||
| async for _ in stream: | ||
| pass | ||
|
|
||
| error = excinfo.value | ||
| assert conversations.calls == 1 | ||
| assert error.status_code == 400 | ||
| assert error.request_id == "req_test" | ||
| assert error.body == '{"message":"provider error"}' | ||
| assert error.retryable is False | ||
| assert len(errors) == 1 | ||
| assert errors[0].error is error | ||
| assert errors[0].recoverable is False | ||
| finally: | ||
| await model.aclose() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_validation_error_is_not_retried(self) -> None: | ||
| conversations = _FakeConversations(lambda _: _validation_error()) | ||
| model = _model(conversations) | ||
|
|
||
| try: | ||
| with pytest.raises(APIStatusError) as excinfo: | ||
| async with model.chat( | ||
| chat_ctx=llm.ChatContext.empty(), | ||
| conn_options=APIConnectOptions(max_retry=3, retry_interval=0.0, timeout=1.0), | ||
| ) as stream: | ||
| async for _ in stream: | ||
| pass | ||
|
|
||
| error = excinfo.value | ||
| assert conversations.calls == 1 | ||
| assert error.status_code == 422 | ||
| assert error.request_id == "req_validation" | ||
| assert error.body == '{"detail":[{"msg":"invalid request"}]}' | ||
| assert error.retryable is False | ||
| finally: | ||
| await model.aclose() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_status_error_after_output_is_not_retried(self) -> None: | ||
| async def _events(): | ||
| yield _event(MessageOutputEvent(id="msg_1", content="partial")) | ||
| raise _sdk_error(500) | ||
|
|
||
| conversations = _FakeConversations(lambda _: _events()) | ||
| model = _model(conversations) | ||
| errors: list[llm.LLMError] = [] | ||
| model.on("error", errors.append) | ||
| chunks: list[llm.ChatChunk] = [] | ||
|
|
||
| try: | ||
| with pytest.raises(APIStatusError) as excinfo: | ||
| async with model.chat( | ||
| chat_ctx=llm.ChatContext.empty(), | ||
| conn_options=APIConnectOptions(max_retry=3, retry_interval=0.0, timeout=1.0), | ||
| ) as stream: | ||
| async for chunk in stream: | ||
| chunks.append(chunk) | ||
|
|
||
| assert [chunk.delta.content for chunk in chunks if chunk.delta] == ["partial"] | ||
| assert conversations.calls == 1 | ||
| assert excinfo.value.status_code == 500 | ||
| assert excinfo.value.retryable is False | ||
| assert len(errors) == 1 | ||
| assert errors[0].recoverable is False | ||
| finally: | ||
| await model.aclose() |
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.
Uh oh!
There was an error while loading. Please reload this page.