Skip to content

fix: handle multiple tool calls in Mistral streaming responses #384

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/strands/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async def stream(self, request: dict[str, Any]) -> AsyncGenerator[dict[str, Any]
yield {"chunk_type": "message_start"}

content_started = False
current_tool_calls: dict[str, dict[str, str]] = {}
tool_calls: dict[str, list[Any]] = {}
accumulated_text = ""

async for chunk in stream_response:
Expand All @@ -440,24 +440,23 @@ async def stream(self, request: dict[str, Any]) -> AsyncGenerator[dict[str, Any]
if hasattr(delta, "tool_calls") and delta.tool_calls:
for tool_call in delta.tool_calls:
tool_id = tool_call.id
tool_calls.setdefault(tool_id, []).append(tool_call)

if tool_id not in current_tool_calls:
yield {"chunk_type": "content_start", "data_type": "tool", "data": tool_call}
current_tool_calls[tool_id] = {"name": tool_call.function.name, "arguments": ""}
if hasattr(choice, "finish_reason") and choice.finish_reason:
if content_started:
yield {"chunk_type": "content_stop", "data_type": "text"}

for tool_deltas in tool_calls.values():
yield {"chunk_type": "content_start", "data_type": "tool", "data": tool_deltas[0]}

if hasattr(tool_call.function, "arguments"):
current_tool_calls[tool_id]["arguments"] += tool_call.function.arguments
for tool_delta in tool_deltas:
if hasattr(tool_delta.function, "arguments"):
yield {
"chunk_type": "content_delta",
"data_type": "tool",
"data": tool_call.function.arguments,
"data": tool_delta.function.arguments,
}

if hasattr(choice, "finish_reason") and choice.finish_reason:
if content_started:
yield {"chunk_type": "content_stop", "data_type": "text"}

for _ in current_tool_calls:
yield {"chunk_type": "content_stop", "data_type": "tool"}

yield {"chunk_type": "message_stop", "data": choice.finish_reason}
Expand Down
21 changes: 6 additions & 15 deletions tests-integ/test_model_mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,32 @@ class Weather(BaseModel):

@pytest.mark.skipif("MISTRAL_API_KEY" not in os.environ, reason="MISTRAL_API_KEY environment variable missing")
def test_agent_invoke(agent):
# TODO: https://github.com/strands-agents/sdk-python/issues/374
# result = streaming_agent("What is the time and weather in New York?")
result = agent("What is the time in New York?")
result = agent("What is the time and weather in New York?")
text = result.message["content"][0]["text"].lower()

# assert all(string in text for string in ["12:00", "sunny"])
assert all(string in text for string in ["12:00"])
assert all(string in text for string in ["12:00", "sunny"])


@pytest.mark.skipif("MISTRAL_API_KEY" not in os.environ, reason="MISTRAL_API_KEY environment variable missing")
@pytest.mark.asyncio
async def test_agent_invoke_async(agent):
# TODO: https://github.com/strands-agents/sdk-python/issues/374
# result = await streaming_agent.invoke_async("What is the time and weather in New York?")
result = await agent.invoke_async("What is the time in New York?")
result = await agent.invoke_async("What is the time and weather in New York?")
text = result.message["content"][0]["text"].lower()

# assert all(string in text for string in ["12:00", "sunny"])
assert all(string in text for string in ["12:00"])
assert all(string in text for string in ["12:00", "sunny"])


@pytest.mark.skipif("MISTRAL_API_KEY" not in os.environ, reason="MISTRAL_API_KEY environment variable missing")
@pytest.mark.asyncio
async def test_agent_stream_async(agent):
# TODO: https://github.com/strands-agents/sdk-python/issues/374
# stream = streaming_agent.stream_async("What is the time and weather in New York?")
stream = agent.stream_async("What is the time in New York?")
stream = agent.stream_async("What is the time and weather in New York?")
async for event in stream:
_ = event

result = event["result"]
text = result.message["content"][0]["text"].lower()

# assert all(string in text for string in ["12:00", "sunny"])
assert all(string in text for string in ["12:00"])
assert all(string in text for string in ["12:00", "sunny"])


@pytest.mark.skipif("MISTRAL_API_KEY" not in os.environ, reason="MISTRAL_API_KEY environment variable missing")
Expand Down