Skip to content

feat: add support for Bedrock/Anthropic ToolChoice to structured_output #720

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/strands/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ..types.content import ContentBlock, Messages
from ..types.exceptions import ContextWindowOverflowException, ModelThrottledException
from ..types.streaming import StreamEvent
from ..types.tools import ToolSpec
from ..types.tools import ToolChoice, ToolSpec
from .model import Model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -192,14 +192,19 @@ def _format_request_messages(self, messages: Messages) -> list[dict[str, Any]]:
return formatted_messages

def format_request(
self, messages: Messages, tool_specs: Optional[list[ToolSpec]] = None, system_prompt: Optional[str] = None
self,
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
) -> dict[str, Any]:
"""Format an Anthropic streaming request.

Args:
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.

Returns:
An Anthropic streaming request.
Expand All @@ -220,6 +225,7 @@ def format_request(
}
for tool_spec in tool_specs or []
],
**({"tool_choice": tool_choice} if tool_choice else {}),
**({"system": system_prompt} if system_prompt else {}),
**(self.config.get("params") or {}),
}
Expand Down Expand Up @@ -347,6 +353,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the Anthropic model.
Expand All @@ -355,6 +362,7 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand All @@ -365,7 +373,7 @@ async def stream(
ModelThrottledException: If the request is throttled by Anthropic.
"""
logger.debug("formatting request")
request = self.format_request(messages, tool_specs, system_prompt)
request = self.format_request(messages, tool_specs, system_prompt, tool_choice)
logger.debug("request=<%s>", request)

logger.debug("invoking model")
Expand Down Expand Up @@ -407,7 +415,13 @@ async def structured_output(
"""
tool_spec = convert_pydantic_to_tool_spec(output_model)

response = self.stream(messages=prompt, tool_specs=[tool_spec], system_prompt=system_prompt, **kwargs)
response = self.stream(
messages=prompt,
tool_specs=[tool_spec],
system_prompt=system_prompt,
tool_choice=cast(ToolChoice, {"any": {}}),
**kwargs,
)
async for event in process_stream(response):
yield event

Expand Down
24 changes: 18 additions & 6 deletions src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import logging
import os
from typing import Any, AsyncGenerator, Callable, Iterable, Literal, Optional, Type, TypeVar, Union
from typing import Any, AsyncGenerator, Callable, Iterable, Literal, Optional, Type, TypeVar, Union, cast

import boto3
from botocore.config import Config as BotocoreConfig
Expand All @@ -20,7 +20,7 @@
from ..types.content import ContentBlock, Message, Messages
from ..types.exceptions import ContextWindowOverflowException, ModelThrottledException
from ..types.streaming import StreamEvent
from ..types.tools import ToolResult, ToolSpec
from ..types.tools import ToolChoice, ToolResult, ToolSpec
from .model import Model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -168,13 +168,15 @@ def format_request(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
) -> dict[str, Any]:
"""Format a Bedrock converse stream request.

Args:
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.

Returns:
A Bedrock converse stream request.
Expand All @@ -197,7 +199,7 @@ def format_request(
else []
),
],
"toolChoice": {"auto": {}},
**({"toolChoice": tool_choice} if tool_choice else {}),
}
}
if tool_specs
Expand Down Expand Up @@ -355,6 +357,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the Bedrock model.
Expand All @@ -366,6 +369,7 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand All @@ -384,7 +388,7 @@ def callback(event: Optional[StreamEvent] = None) -> None:
loop = asyncio.get_event_loop()
queue: asyncio.Queue[Optional[StreamEvent]] = asyncio.Queue()

thread = asyncio.to_thread(self._stream, callback, messages, tool_specs, system_prompt)
thread = asyncio.to_thread(self._stream, callback, messages, tool_specs, system_prompt, tool_choice)
task = asyncio.create_task(thread)

while True:
Expand All @@ -402,6 +406,7 @@ def _stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
) -> None:
"""Stream conversation with the Bedrock model.

Expand All @@ -413,14 +418,15 @@ def _stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.

Raises:
ContextWindowOverflowException: If the input exceeds the model's context window.
ModelThrottledException: If the model service is throttling requests.
"""
try:
logger.debug("formatting request")
request = self.format_request(messages, tool_specs, system_prompt)
request = self.format_request(messages, tool_specs, system_prompt, tool_choice)
logger.debug("request=<%s>", request)

logger.debug("invoking model")
Expand Down Expand Up @@ -624,7 +630,13 @@ async def structured_output(
"""
tool_spec = convert_pydantic_to_tool_spec(output_model)

response = self.stream(messages=prompt, tool_specs=[tool_spec], system_prompt=system_prompt, **kwargs)
response = self.stream(
messages=prompt,
tool_specs=[tool_spec],
system_prompt=system_prompt,
tool_choice=cast(ToolChoice, {"any": {}}),
**kwargs,
)
async for event in streaming.process_stream(response):
yield event

Expand Down
5 changes: 4 additions & 1 deletion src/strands/models/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from ..types.content import ContentBlock, Messages
from ..types.streaming import StreamEvent
from ..types.tools import ToolSpec
from ..types.tools import ToolChoice, ToolSpec
from .openai import OpenAIModel

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -109,6 +109,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the LiteLLM model.
Expand All @@ -117,6 +118,8 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
interface consistency but is currently ignored for this model provider.**
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand Down
5 changes: 4 additions & 1 deletion src/strands/models/llamaapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ..types.content import ContentBlock, Messages
from ..types.exceptions import ModelThrottledException
from ..types.streaming import StreamEvent, Usage
from ..types.tools import ToolResult, ToolSpec, ToolUse
from ..types.tools import ToolChoice, ToolResult, ToolSpec, ToolUse
from .model import Model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -327,6 +327,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the LlamaAPI model.
Expand All @@ -335,6 +336,8 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
interface consistency but is currently ignored for this model provider.**
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand Down
5 changes: 4 additions & 1 deletion src/strands/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..types.content import ContentBlock, Messages
from ..types.exceptions import ModelThrottledException
from ..types.streaming import StopReason, StreamEvent
from ..types.tools import ToolResult, ToolSpec, ToolUse
from ..types.tools import ToolChoice, ToolResult, ToolSpec, ToolUse
from .model import Model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -394,6 +394,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the Mistral model.
Expand All @@ -402,6 +403,8 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
interface consistency but is currently ignored for this model provider.**
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand Down
4 changes: 3 additions & 1 deletion src/strands/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..types.content import Messages
from ..types.streaming import StreamEvent
from ..types.tools import ToolSpec
from ..types.tools import ToolChoice, ToolSpec

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,6 +70,7 @@ def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncIterable[StreamEvent]:
"""Stream conversation with the model.
Expand All @@ -84,6 +85,7 @@ def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation.
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand Down
5 changes: 4 additions & 1 deletion src/strands/models/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ..types.content import ContentBlock, Messages
from ..types.streaming import StopReason, StreamEvent
from ..types.tools import ToolSpec
from ..types.tools import ToolChoice, ToolSpec
from .model import Model

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -284,6 +284,7 @@ async def stream(
messages: Messages,
tool_specs: Optional[list[ToolSpec]] = None,
system_prompt: Optional[str] = None,
tool_choice: Optional[ToolChoice] = None,
**kwargs: Any,
) -> AsyncGenerator[StreamEvent, None]:
"""Stream conversation with the Ollama model.
Expand All @@ -292,6 +293,8 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
interface consistency but is currently ignored for this model provider.**
**kwargs: Additional keyword arguments for future extensibility.

Yields:
Expand Down
Loading