Skip to content

feat(mistralai): add chat completions API mode - #7381

Merged
tinalenguyen merged 5 commits into
livekit:mainfrom
leamoukarzel:lea/mistralai-chat-completions-api-mode
Sep 22, 2026
Merged

tinalenguyen merged 5 commits into
livekit:mainfrom
leamoukarzel:lea/mistralai-chat-completions-api-mode

Conversation

@leamoukarzel

@leamoukarzel leamoukarzel commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Why

The Mistral Chat Completions API (/v1/chat/completions) delivers significantly lower p50 TTFT than the Conversations API (/v1/conversations): -21% on mistral-small (334 ms vs 421 ms) and -36% on mistral-medium (450 ms vs 707 ms).

However, Chat Completions only supports function tools — provider tools (web search, document library, code interpreter, connectors) require the Conversations API. This PR adds an api_mode switch so callers can opt into the faster path when they don't need provider tools, while keeping Conversations as the default.

Summary

Adds an api_mode parameter to the MistralAI LLM plugin, allowing callers to choose between:

  • ApiMode.CONVERSATIONS (default) — uses the Conversations API (/v1/conversations), which supports both function tools and provider tools (web search, document library, code interpreter, connectors).
  • ApiMode.CHAT_COMPLETIONS — uses the Chat Completions API (/v1/chat/completions), which only supports function tools but has lower latency.

The default remains CONVERSATIONS, so this is fully backward-compatible.

Changes

  • New ApiMode enum exported from livekit.plugins.mistralai
  • LLM.__init__ and update_options accept an api_mode parameter
  • LLMStream._run dispatches to _run_conversations (existing logic, unchanged) or _run_chat_completions (new)
  • Chat completions mode validates that no provider tools are passed (raises ValueError)
  • Streaming delta parsing and tool call accumulation for the chat completions response format
  • Comprehensive test coverage for the new mode

Test plan

  • Existing conversations-mode tests still pass
  • New TestChatCompletionsMode tests cover text streaming, tool call accumulation, usage reporting, error mapping, and provider tool rejection
  • Manual integration test against Mistral API

Add `ApiMode` enum (`CONVERSATIONS` | `CHAT_COMPLETIONS`) to the Mistral
plugin so callers can opt into the lower-latency Chat Completions API for
sessions that only use function tools.

Fix a bug in `_parse_completion_delta` where a redundant `if` guard
caused tool-call arguments to be doubled on the first streamed delta.

Remove dead `_emitted_tool_calls` / `_provider_tool_args` assignments
from the chat completions path and add a test for provider-tool rejection.
@CLAassistant

CLAassistant commented Sep 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@leamoukarzel
leamoukarzel marked this pull request as ready for review September 21, 2026 15:31
@leamoukarzel
leamoukarzel requested a review from a team as a code owner September 21, 2026 15:31
devin-ai-integration[bot]

This comment was marked as resolved.

@tinalenguyen
tinalenguyen merged commit f55d5ff into livekit:main Sep 22, 2026
15 checks passed
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