Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(providers): add tool_choice parameter to Provider protocol and all implementations

xf gong80053dfed7a9
diff --git a/cubepi/providers/anthropic.py b/cubepi/providers/anthropic.py
index 2939329..8dd1737 100644
--- a/cubepi/providers/anthropic.py
+++ b/cubepi/providers/anthropic.py
@@ -18,6 +18,7 @@ from cubepi.providers.base import (
TextContent,
ThinkingContent,
ToolCall,
+ ToolChoice,
ToolDefinition,
ToolResultMessage,
Usage,
@@ -138,6 +139,16 @@ class AnthropicProvider(BaseProvider):
def _resolve_capability(self, model_id: str) -> CapabilityDescriptor:
return self._model_overrides.get(model_id, self._capability)
+ @staticmethod
+ def _map_tool_choice(choice: str) -> dict[str, str] | None:
+ if choice == "auto":
+ return {"type": "auto"}
+ if choice == "required":
+ return {"type": "any"}
+ if choice == "none":
+ return None
+ return {"type": "tool", "name": choice}
+
async def stream(
self,
model: Model,
@@ -145,6 +156,7 @@ class AnthropicProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
opts = options or StreamOptions()
@@ -186,6 +198,11 @@ class AnthropicProvider(BaseProvider):
api_tools[-1]["cache_control"] = cache_control
kwargs["tools"] = api_tools
+ if tool_choice is not None and tool_choice != "none":
+ mapped = self._map_tool_choice(tool_choice)
+ if mapped is not None:
+ kwargs["tool_choice"] = mapped
+
# Capability-driven thinking + temperature. The default capability
# (capability=None) reproduces today's wire bytes:
# - thinking off: reasoning_off_payload is empty, so no "thinking"
diff --git a/cubepi/providers/base.py b/cubepi/providers/base.py
index 589da86..9b88473 100644
--- a/cubepi/providers/base.py
+++ b/cubepi/providers/base.py
@@ -20,6 +20,8 @@ from cubepi.types import JsonObject, StructuredValue
ThinkingLevel = Literal["off", "minimal", "low", "medium", "high", "xhigh"]
+ToolChoice = Literal["auto", "required", "none"] | str
+
class ThinkingBudgets(BaseModel):
"""Token budgets for each thinking level."""
@@ -101,6 +103,7 @@ class BoundModel:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
# Forward ``model`` and ``messages`` positionally so a custom
@@ -114,6 +117,7 @@ class BoundModel:
messages,
system_prompt=system_prompt,
tools=tools,
+ tool_choice=tool_choice,
options=options,
)
@@ -123,6 +127,7 @@ class BoundModel:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -135,6 +140,7 @@ class BoundModel:
messages,
system_prompt=system_prompt,
tools=tools,
+ tool_choice=tool_choice,
options=options,
max_output_tokens=max_output_tokens,
temperature=temperature,
@@ -632,6 +638,7 @@ class Provider(Protocol):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream: ...
@@ -642,6 +649,7 @@ class Provider(Protocol):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -710,6 +718,7 @@ class BaseProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
raise NotImplementedError
@@ -721,6 +730,7 @@ class BaseProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -750,6 +760,7 @@ class BaseProvider:
messages=messages,
system_prompt=system_prompt,
tools=tools,
+ tool_choice=tool_choice,
options=options,
)
async for event in stream:
diff --git a/cubepi/providers/faux.py b/cubepi/providers/faux.py
index 6de76a4..d50c9cb 100644
--- a/cubepi/providers/faux.py
+++ b/cubepi/providers/faux.py
@@ -19,6 +19,7 @@ from cubepi.providers.base import (
TextContent,
ThinkingContent,
ToolCall,
+ ToolChoice,
ToolDefinition,
Usage,
_fire_request_listeners,
@@ -271,6 +272,7 @@ class FauxProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
opts = options or StreamOptions()
diff --git a/cubepi/providers/openai.py b/cubepi/providers/openai.py
index 55aeebb..f5883dd 100644
--- a/cubepi/providers/openai.py
+++ b/cubepi/providers/openai.py
@@ -26,6 +26,7 @@ from cubepi.providers.base import (
TextContent,
ThinkingContent,
ToolCall,
+ ToolChoice,
ToolDefinition,
ToolResultMessage,
Usage,
@@ -76,6 +77,12 @@ class OpenAIProvider(BaseProvider):
def _resolve_capability(self, model_id: str) -> CapabilityDescriptor:
return self._model_overrides.get(model_id, self._capability)
+ @staticmethod
+ def _map_tool_choice(choice: str) -> str | dict:
+ if choice in ("auto", "required", "none"):
+ return choice
+ return {"type": "function", "function": {"name": choice}}
+
async def stream(
self,
model: Model,
@@ -83,6 +90,7 @@ class OpenAIProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
opts = options or StreamOptions()
@@ -106,6 +114,9 @@ class OpenAIProvider(BaseProvider):
if tools:
kwargs["tools"] = [self._convert_tool(t) for t in tools]
+ if tool_choice is not None:
+ kwargs["tool_choice"] = self._map_tool_choice(tool_choice)
+
async def _produce() -> None:
body: dict | None = None
exc: BaseException | None = None
diff --git a/cubepi/providers/openai_responses.py b/cubepi/providers/openai_responses.py
index fa4aab5..9473f5a 100644
--- a/cubepi/providers/openai_responses.py
+++ b/cubepi/providers/openai_responses.py
@@ -19,6 +19,7 @@ from cubepi.providers.base import (
ThinkingContent,
ThinkingLevel,
ToolCall,
+ ToolChoice,
ToolDefinition,
ToolResultMessage,
Usage,
@@ -88,6 +89,12 @@ class OpenAIResponsesProvider(BaseProvider):
def _resolve_capability(self, model_id: str) -> CapabilityDescriptor:
return self._model_overrides.get(model_id, self._capability)
+ @staticmethod
+ def _map_tool_choice(choice: str) -> str | dict:
+ if choice in ("auto", "required", "none"):
+ return choice
+ return {"type": "function", "name": choice}
+
@staticmethod
def _assemble_response(resp: Any) -> dict[str, Any] | None:
"""Convert the OpenAI Responses SDK Response object to its canonical
@@ -106,6 +113,7 @@ class OpenAIResponsesProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: ToolChoice | None = None,
options: StreamOptions | None = None,
) -> MessageStream:
opts = options or StreamOptions()
@@ -127,6 +135,9 @@ class OpenAIResponsesProvider(BaseProvider):
if tools:
kwargs["tools"] = [self._convert_tool(t) for t in tools]
+ if tool_choice is not None:
+ kwargs["tool_choice"] = self._map_tool_choice(tool_choice)
+
async def _produce() -> None:
body: dict | None = None
exc: BaseException | None = None
diff --git a/tests/agent/test_loop.py b/tests/agent/test_loop.py
index 692e585..4763f67 100644
--- a/tests/agent/test_loop.py
+++ b/tests/agent/test_loop.py
@@ -609,6 +609,7 @@ class _NoFinalEventProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
self.call_count += 1
@@ -664,6 +665,7 @@ class _NoFinalEventNoPartialProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
self.call_count += 1
diff --git a/tests/middleware/compaction/test_summarizer.py b/tests/middleware/compaction/test_summarizer.py
index 37bc171..ec2e841 100644
--- a/tests/middleware/compaction/test_summarizer.py
+++ b/tests/middleware/compaction/test_summarizer.py
@@ -33,6 +33,7 @@ class _FakeProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -106,13 +107,14 @@ async def test_summarize_raises_on_provider_error_message() -> None:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
thinking=None,
thinking_budgets=None,
) -> AssistantMessage:
- del model, messages, system_prompt, tools, options
+ del model, messages, system_prompt, tools, tool_choice, options
del max_output_tokens, temperature, thinking, thinking_budgets
return AssistantMessage(
content=[],
diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.py
index a451438..bdad016 100644
--- a/tests/middleware/test_compaction.py
+++ b/tests/middleware/test_compaction.py
@@ -42,6 +42,7 @@ class _FakeSummaryProvider:
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
diff --git a/tests/providers/test_base.py b/tests/providers/test_base.py
index df7faf3..957887e 100644
--- a/tests/providers/test_base.py
+++ b/tests/providers/test_base.py
@@ -1,4 +1,5 @@
import asyncio
+from typing import Any
import pytest
@@ -312,9 +313,10 @@ class _RecordingProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
- del messages, system_prompt, tools
+ del messages, system_prompt, tools, tool_choice
self.call_count += 1
self.seen_model = model
self.seen_options = options
diff --git a/tests/providers/test_bound_model_calls.py b/tests/providers/test_bound_model_calls.py
index bb44626..2c3524e 100644
--- a/tests/providers/test_bound_model_calls.py
+++ b/tests/providers/test_bound_model_calls.py
@@ -29,6 +29,7 @@ class _RecordingProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -128,6 +129,7 @@ class _MangledNamesProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
diff --git a/tests/providers/test_fallback.py b/tests/providers/test_fallback.py
index 6c44af4..8735bd2 100644
--- a/tests/providers/test_fallback.py
+++ b/tests/providers/test_fallback.py
@@ -45,6 +45,7 @@ class _RaisingProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
raise self._error
@@ -56,6 +57,7 @@ class _RaisingProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
@@ -275,6 +277,7 @@ class _EmptyStreamProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
ms = MessageStream()
@@ -312,6 +315,7 @@ class _MidStreamErrorProvider(BaseProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
ms = MessageStream()
diff --git a/tests/tracing/test_oneshot.py b/tests/tracing/test_oneshot.py
index 6fe9351..2cc15a7 100644
--- a/tests/tracing/test_oneshot.py
+++ b/tests/tracing/test_oneshot.py
@@ -698,13 +698,14 @@ class _GenerateOnlyProvider(FauxProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
max_output_tokens: int | None = None,
temperature: float | None = None,
thinking=None,
thinking_budgets=None,
) -> AssistantMessage:
- del model, messages, system_prompt, tools, temperature, thinking
+ del model, messages, system_prompt, tools, tool_choice, temperature, thinking
del thinking_budgets
self.seen_max_output_tokens = max_output_tokens
self.seen_options = options
@@ -717,7 +718,8 @@ class _GenerateOnlyProvider(FauxProvider):
*,
system_prompt: str = "",
tools: list[ToolDefinition] | None = None,
+ tool_choice: Any = None,
options: StreamOptions | None = None,
) -> MessageStream:
- del model, messages, system_prompt, tools, options
+ del model, messages, system_prompt, tools, tool_choice, options
raise AssertionError("Tracer.oneshot must call provider.generate()")