-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat(mistral): migrate LLM to Conversations API with provider tools support #5527
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 6 commits into
livekit:main
from
jeanprbt:jean/feat/add-mistral-provider-tools
Apr 22, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e023916
feat(mistral): add provider tools support
jeanprbt 78cc204
feat(mistral): support all llm completion params
jeanprbt be78042
chore(mistral): switch back default tts response format to mp3
jeanprbt 0679fc8
fix(mistral): apply devin review suggestions
jeanprbt 8c99f1e
fix(mistral): apply devin review suggestions (x2)
jeanprbt b27a1c8
fix(mistral): apply devin review suggestions (x3)
jeanprbt 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
110 changes: 101 additions & 9 deletions
110
livekit-agents/livekit/agents/llm/_provider_format/mistralai.py
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 |
|---|---|---|
| @@ -1,17 +1,109 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal | ||
| import base64 | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| from livekit.agents import llm | ||
|
|
||
| from .openai import to_chat_ctx as openai_to_chat_ctx | ||
| from .utils import group_tool_calls | ||
|
|
||
|
|
||
| def to_chat_ctx( | ||
| chat_ctx: llm.ChatContext, *, inject_dummy_user_message: bool = True | ||
| ) -> tuple[list[dict], Literal[None]]: | ||
| messages, _ = openai_to_chat_ctx(chat_ctx, inject_dummy_user_message=inject_dummy_user_message) | ||
| @dataclass | ||
| class MistralFormatData: | ||
| instructions: str | None | ||
|
|
||
| if inject_dummy_user_message and (not messages or messages[-1]["role"] not in ["user", "tool"]): | ||
| messages.append({"role": "user", "content": ""}) | ||
| return messages, None | ||
|
|
||
| def to_conversations_ctx( | ||
| chat_ctx: llm.ChatContext, | ||
| ) -> tuple[list[dict], MistralFormatData]: | ||
| """Convert ChatContext to Mistral Conversations API entry format. | ||
|
|
||
| Returns: | ||
| A tuple of (entries, instructions) where instructions is the extracted | ||
| system/developer message content (or None if absent). | ||
| """ | ||
| item_groups = group_tool_calls(chat_ctx) | ||
| entries: list[dict[str, Any]] = [] | ||
| instructions: str | None = None | ||
|
|
||
| for group in item_groups: | ||
| if not group.message and not group.tool_calls and not group.tool_outputs: | ||
| continue | ||
|
|
||
| if group.message: | ||
| item = group.message | ||
| if isinstance(item, llm.ChatMessage) and item.role in ("system", "developer"): | ||
| text_parts = [c for c in item.content if isinstance(c, str)] | ||
| instructions = "\n".join(text_parts) if text_parts else None | ||
| continue | ||
|
|
||
| entry = _to_entry(item) | ||
| if entry is not None: | ||
| entries.append(entry) | ||
|
|
||
| for tool_call in group.tool_calls: | ||
| entries.append( | ||
| { | ||
| "type": "function.call", | ||
| "tool_call_id": tool_call.call_id, | ||
| "name": tool_call.name, | ||
| "arguments": tool_call.arguments, | ||
| } | ||
| ) | ||
|
|
||
| for tool_output in group.tool_outputs: | ||
| entries.append( | ||
| { | ||
| "type": "function.result", | ||
| "tool_call_id": tool_output.call_id, | ||
| "result": tool_output.output, | ||
| } | ||
| ) | ||
|
|
||
| return entries, MistralFormatData(instructions=instructions) | ||
|
|
||
|
|
||
| def _to_entry(item: llm.ChatItem) -> dict[str, Any] | None: | ||
| if not isinstance(item, llm.ChatMessage): | ||
| return None | ||
|
|
||
| content = _build_content(item) | ||
|
|
||
| if item.role == "user": | ||
| return {"type": "message.input", "role": "user", "content": content} | ||
| elif item.role == "assistant": | ||
| return {"type": "message.output", "role": "assistant", "content": content} | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def _build_content(msg: llm.ChatMessage) -> str | list[dict[str, Any]]: | ||
| list_content: list[dict[str, Any]] = [] | ||
| text_content = "" | ||
|
|
||
| for content in msg.content: | ||
| if isinstance(content, str): | ||
| if text_content: | ||
| text_content += "\n" | ||
| text_content += content | ||
| elif isinstance(content, llm.ImageContent): | ||
| list_content.append(_to_image_content(content)) | ||
|
|
||
| if not list_content: | ||
| return text_content | ||
|
|
||
| if text_content: | ||
| list_content.append({"type": "text", "text": text_content}) | ||
|
|
||
| return list_content | ||
|
|
||
|
|
||
| def _to_image_content(image: llm.ImageContent) -> dict[str, Any]: | ||
| img = llm.utils.serialize_image(image) | ||
| if img.external_url: | ||
| return {"type": "image_url", "image_url": img.external_url} | ||
|
|
||
| assert img.data_bytes is not None | ||
| b64_data = base64.b64encode(img.data_bytes).decode("utf-8") | ||
| return {"type": "image_url", "image_url": f"data:{img.mime_type};base64,{b64_data}"} | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.