From 18fcb196a22f948e7405ae7d32c612b915325e8b Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Mon, 2 Mar 2026 21:01:13 +0100 Subject: [PATCH] docs(migration): fix async streaming example with complete runnable code Both async examples (v0.x and v1.x) were missing the async def main() wrapper and asyncio.run() call, making them non-functional as standalone scripts. Added import asyncio, async def main() wrapper, asyncio.run() entry point, and a note about environments with active event loops. Fixes #294 --- MIGRATION.md | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/MIGRATION.md b/MIGRATION.md index 2fc3d13d..3d0f60eb 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -165,23 +165,33 @@ for chunk in client.chat.stream(model="mistral-large-latest", messages=messages) **v0.x:** ```python +import asyncio from mistralai.async_client import MistralAsyncClient from mistralai.models.chat_completion import ChatMessage -client = MistralAsyncClient(api_key=api_key) -messages = [ChatMessage(role="user", content="What is the best French cheese?")] +async def main(): + client = MistralAsyncClient(api_key=api_key) + messages = [ChatMessage(role="user", content="What is the best French cheese?")] -async for chunk in client.chat_stream(model="mistral-large-latest", messages=messages): - print(chunk.choices[0].delta.content) + async for chunk in client.chat_stream(model="mistral-large-latest", messages=messages): + print(chunk.choices[0].delta.content) + +asyncio.run(main()) ``` **v1.x:** ```python +import asyncio from mistralai import Mistral, UserMessage -client = Mistral(api_key=api_key) -messages = [UserMessage(content="What is the best French cheese?")] +async def main(): + client = Mistral(api_key=api_key) + messages = [UserMessage(content="What is the best French cheese?")] + + async for chunk in await client.chat.stream_async(model="mistral-large-latest", messages=messages): + print(chunk.data.choices[0].delta.content) -async for chunk in await client.chat.stream_async(model="mistral-large-latest", messages=messages): - print(chunk.data.choices[0].delta.content) +asyncio.run(main()) ``` + +> **Note:** If you are running in an environment with an already-active event loop (e.g., Jupyter notebooks, Positron IDE, VS Code interactive window), use `await main()` instead of `asyncio.run(main())`.