diff --git a/libs/partners/mistralai/langchain_mistralai/chat_models.py b/libs/partners/mistralai/langchain_mistralai/chat_models.py index 6d6bbda439dd5..4e8c548fc29ed 100644 --- a/libs/partners/mistralai/langchain_mistralai/chat_models.py +++ b/libs/partners/mistralai/langchain_mistralai/chat_models.py @@ -347,6 +347,15 @@ async def _completion_with_retry(**kwargs: Any) -> Any: return await _completion_with_retry(**kwargs) +def _is_empty_chunk(chunk: AIMessageChunk) -> bool: + return ( + not chunk.content + and not chunk.tool_call_chunks + and not chunk.usage_metadata + and "finish_reason" not in chunk.response_metadata + ) + + def _convert_chunk_to_message_chunk( chunk: dict, default_class: type[BaseMessageChunk], @@ -358,8 +367,9 @@ def _convert_chunk_to_message_chunk( _delta = _choice["delta"] role = _delta.get("role") content = _delta.get("content") or "" - if output_version == "v1" and isinstance(content, str): - content = [{"type": "text", "text": content}] + # Once a stream has sent list content, keep later string deltas as text blocks + if isinstance(content, str) and (output_version == "v1" or index_type): + content = [{"type": "text", "text": content}] if content else [] content = _normalize_mistral_content(content) if isinstance(content, list): for block in content: @@ -667,6 +677,16 @@ class ChatMistralAI(BaseChatModel): safe_mode: bool | None = None + reasoning_effort: str | None = None + """Reasoning effort. + + Forwarded as the `reasoning_effort` request field. Supported values vary by + model (e.g. `'none'` or `'high'`). + + Can also be passed at call time, e.g. + `model.invoke(..., reasoning_effort="high")`. + """ + streaming: bool = False model_kwargs: dict[str, Any] = Field(default_factory=dict) @@ -694,6 +714,7 @@ def _default_params(self) -> dict[str, Any]: "top_p": self.top_p, "random_seed": self.random_seed, "safe_prompt": self.safe_mode, + "reasoning_effort": self.reasoning_effort, **self.model_kwargs, } return {k: v for k, v in defaults.items() if v is not None} @@ -917,6 +938,8 @@ def _stream( ) # make future chunks same type as first chunk default_chunk_class = new_chunk.__class__ + if isinstance(new_chunk, AIMessageChunk) and _is_empty_chunk(new_chunk): + continue gen_chunk = ChatGenerationChunk(message=new_chunk) if run_manager: run_manager.on_llm_new_token( @@ -947,6 +970,8 @@ async def _astream( ) # make future chunks same type as first chunk default_chunk_class = new_chunk.__class__ + if isinstance(new_chunk, AIMessageChunk) and _is_empty_chunk(new_chunk): + continue gen_chunk = ChatGenerationChunk(message=new_chunk) if run_manager: await run_manager.on_llm_new_token( diff --git a/libs/partners/mistralai/tests/integration_tests/test_chat_models.py b/libs/partners/mistralai/tests/integration_tests/test_chat_models.py index 4ca23b5aeb488..9945e0ff51e49 100644 --- a/libs/partners/mistralai/tests/integration_tests/test_chat_models.py +++ b/libs/partners/mistralai/tests/integration_tests/test_chat_models.py @@ -150,7 +150,11 @@ def test_retry_parameters(caplog: pytest.LogCaptureFixture) -> None: def test_reasoning() -> None: - model = ChatMistralAI(model="magistral-medium-latest", rate_limiter=rate_limiter) # type: ignore[call-arg] + model = ChatMistralAI( # type: ignore[call-arg] + model="magistral-medium-latest", + reasoning_effort="high", + rate_limiter=rate_limiter, + ) input_message = { "role": "user", "content": "Hello, my name is Bob.", @@ -176,6 +180,7 @@ def test_reasoning() -> None: def test_reasoning_v1() -> None: model = ChatMistralAI( # type: ignore[call-arg] model="magistral-medium-latest", + reasoning_effort="high", output_version="v1", rate_limiter=rate_limiter, ) diff --git a/libs/partners/mistralai/tests/integration_tests/test_standard.py b/libs/partners/mistralai/tests/integration_tests/test_standard.py index c1281898ea924..59c82554a929e 100644 --- a/libs/partners/mistralai/tests/integration_tests/test_standard.py +++ b/libs/partners/mistralai/tests/integration_tests/test_standard.py @@ -19,7 +19,6 @@ def chat_model_class(self) -> type[BaseChatModel]: def chat_model_params(self) -> dict: return { "model": "mistral-large-latest", - "temperature": 0, "rate_limiter": rate_limiter, } diff --git a/libs/partners/mistralai/tests/unit_tests/test_chat_models.py b/libs/partners/mistralai/tests/unit_tests/test_chat_models.py index 09120c2016c6a..5f7fb778c3d88 100644 --- a/libs/partners/mistralai/tests/unit_tests/test_chat_models.py +++ b/libs/partners/mistralai/tests/unit_tests/test_chat_models.py @@ -1,7 +1,7 @@ """Test MistralAI Chat API wrapper.""" import os -from collections.abc import AsyncGenerator, Generator +from collections.abc import AsyncGenerator, Generator, Iterator from typing import TYPE_CHECKING, Any, cast from unittest.mock import MagicMock, patch @@ -1230,3 +1230,134 @@ def test_langsmith_gateway_context_overrides_explicit_values() -> None: assert llm.endpoint == "https://gateway.smith.langchain.com/mistral/v1" assert llm.mistral_api_key is not None assert llm.mistral_api_key.get_secret_value() == "service-key" + + +def _make_delta_chunk(content: Any, finish_reason: str | None = None) -> dict: + return { + "id": "abc123", + "model": "mistral-large-latest", + "choices": [ + {"index": 0, "delta": {"content": content}, "finish_reason": finish_reason} + ], + } + + +_REASONING_STREAM = [ + { + "id": "abc123", + "model": "mistral-large-latest", + "choices": [ + { + "index": 0, + "delta": {"role": "assistant", "content": ""}, + "finish_reason": None, + } + ], + }, + _make_delta_chunk( + [{"type": "thinking", "thinking": [{"type": "text", "text": "User says"}]}] + ), + _make_delta_chunk( + [{"type": "thinking", "thinking": [{"type": "text", "text": " hello."}]}] + ), + _make_delta_chunk( + [ + {"type": "thinking", "thinking": []}, + {"type": "text", "text": "Hello! How"}, + ] + ), + _make_delta_chunk(" can I help you"), + _make_delta_chunk(" today?", finish_reason="stop"), +] + + +def mock_reasoning_stream(*args: Any, **kwargs: Any) -> Iterator[dict]: + return iter([dict(c) for c in _REASONING_STREAM]) + + +async def mock_reasoning_astream(*args: Any, **kwargs: Any) -> AsyncGenerator: + async def it() -> AsyncGenerator: + for c in _REASONING_STREAM: + yield dict(c) + + return it() + + +def _check_reasoning_stream(chunks: list[AIMessageChunk]) -> None: + assert all(c.content for c in chunks[:-1]) + full = chunks[0] + for c in chunks[1:]: + full += c + assert isinstance(full, AIMessageChunk) + text_blocks = [b for b in full.content_blocks if b["type"] == "text"] + assert len(text_blocks) == 1 + assert text_blocks[0]["text"] == "Hello! How can I help you today?" + reasoning_blocks = [b for b in full.content_blocks if b["type"] == "reasoning"] + assert len(reasoning_blocks) == 1 + assert reasoning_blocks[0].get("reasoning") == "User says hello." + assert full.text == "Hello! How can I help you today?" + assert full.chunk_position == "last" + + +@pytest.mark.parametrize("output_version", [None, "v1"]) +@patch( + "langchain_mistralai.chat_models.ChatMistralAI.completion_with_retry", + new=mock_reasoning_stream, +) +def test_stream_reasoning_then_text(output_version: str | None) -> None: + llm = ChatMistralAI(output_version=output_version) + chunks = [cast("AIMessageChunk", c) for c in llm.stream("Hello")] + _check_reasoning_stream(chunks) + + +@pytest.mark.parametrize("output_version", [None, "v1"]) +@patch( + "langchain_mistralai.chat_models.acompletion_with_retry", + new=mock_reasoning_astream, +) +async def test_astream_reasoning_then_text(output_version: str | None) -> None: + llm = ChatMistralAI(output_version=output_version) + chunks = [cast("AIMessageChunk", c) async for c in llm.astream("Hello")] + _check_reasoning_stream(chunks) + + +@patch( + "langchain_mistralai.chat_models.ChatMistralAI.completion_with_retry", + new=mock_chat_stream, +) +def test_stream_string_content_stays_string() -> None: + llm = ChatMistralAI() + full: AIMessageChunk | None = None + for chunk in llm.stream("Hello"): + full = chunk if full is None else full + chunk # type: ignore[assignment] + assert full is not None + assert full.content == "Hello how can I help?" + + +def test_reasoning_effort_omitted_by_default() -> None: + llm = ChatMistralAI() + assert "reasoning_effort" not in llm._default_params + + +def test_reasoning_effort_in_default_params() -> None: + llm = ChatMistralAI(reasoning_effort="high") + assert llm._default_params["reasoning_effort"] == "high" + + +def test_reasoning_effort_passed_to_client() -> None: + llm = ChatMistralAI(reasoning_effort="high") + with patch.object( + ChatMistralAI, + "completion_with_retry", + return_value={ + "choices": [ + { + "message": {"role": "assistant", "content": "Hi"}, + "finish_reason": "stop", + } + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }, + ) as mock_completion: + llm.invoke("Hello") + assert mock_completion.call_args.kwargs["reasoning_effort"] == "high"