Skip to content

Commit c997ff7

Browse files
authored
Fix tool_choice issue (#2543)
## Description We default tool_choice for teams in the different modes. Users need to be able to choose whether it needs to be required or not. --- ## Type of change Please check the options that are relevant: - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Model update (Addition or modification of models) - [ ] Other (please describe): --- ## Checklist - [ ] Adherence to standards: Code complies with Agno’s style guidelines and best practices. - [ ] Formatting and validation: You have run `./scripts/format.sh` and `./scripts/validate.sh` to ensure code is formatted and linted. - [ ] Self-review completed: A thorough review has been performed by the contributor(s). - [ ] Documentation: Docstrings and comments have been added or updated for any complex logic. - [ ] Examples and guides: Relevant cookbook examples have been included or updated (if applicable). - [ ] Tested in a clean environment: Changes have been tested in a clean environment to confirm expected behavior. - [ ] Tests (optional): Tests have been added or updated to cover any new or changed functionality. --- ## Additional Notes Include any deployment notes, performance implications, security considerations, or other relevant information (e.g., screenshots or logs if applicable).
1 parent c02df64 commit c997ff7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

cookbook/examples/teams/route/multi_language_team.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
from agno.models.openai import OpenAIChat
66
from agno.team.team import Team
77

8-
english_agent = Agent(
9-
name="English Agent",
10-
role="You only answer in English",
11-
model=OpenAIChat(id="gpt-4o"),
12-
)
138
japanese_agent = Agent(
149
name="Japanese Agent",
1510
role="You only answer in Japanese",
@@ -41,7 +36,6 @@
4136
mode="route",
4237
model=OpenAIChat("gpt-4o"),
4338
members=[
44-
english_agent,
4539
spanish_agent,
4640
japanese_agent,
4741
french_agent,
@@ -52,8 +46,9 @@
5246
instructions=[
5347
"Identify the language of the user's question and direct it to the appropriate language agent.",
5448
"Let the language agent answer the question in the language of the user's question.",
55-
"If the user asks in a language whose agent is not a team member, respond in English with:",
56-
"'I only answer in the following languages: English, Spanish, Japanese, French and German. Please ask your question in one of these languages.'",
49+
"The the user asks a question in English, respond directly in English with:",
50+
"If the user asks in a language that is not English or your don't have a member agent for that language, respond in English with:",
51+
"'I only answer in the following languages: English, Spanish, Japanese, Chinese, French and German. Please ask your question in one of these languages.'",
5752
"Always check the language of the user's input before routing to an agent.",
5853
"For unsupported languages like Italian, respond in English with the above message.",
5954
],

libs/agno/agno/team/team.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class Team:
134134

135135
# Show tool calls in Team response. This sets the default for the team.
136136
show_tool_calls: bool = True
137+
# Controls which (if any) tool is called by the team model.
138+
# "none" means the model will not call a tool and instead generates a message.
139+
# "auto" means the model can pick between generating a message or calling a tool.
140+
# Specifying a particular function via {"type: "function", "function": {"name": "my_function"}}
141+
# forces the model to call that tool.
142+
# "none" is the default when no tools are present. "auto" is the default if tools are present.
143+
tool_choice: Optional[Union[str, Dict[str, Any]]] = None
137144

138145
# --- Structured output ---
139146
# Response model for the team response
@@ -197,6 +204,7 @@ def __init__(
197204
share_member_interactions: bool = False,
198205
read_team_history: bool = False,
199206
show_tool_calls: bool = True,
207+
tool_choice: Optional[Union[str, Dict[str, Any]]] = None,
200208
response_model: Optional[Type[BaseModel]] = None,
201209
use_json_mode: bool = False,
202210
parse_response: bool = True,
@@ -243,7 +251,9 @@ def __init__(
243251
self.share_member_interactions = share_member_interactions
244252

245253
self.read_team_history = read_team_history
254+
246255
self.show_tool_calls = show_tool_calls
256+
self.tool_choice = tool_choice
247257

248258
self.response_model = response_model
249259
self.use_json_mode = use_json_mode
@@ -477,7 +487,7 @@ def run(
477487
files=files, # type: ignore
478488
)
479489
_built_in_tools.append(forward_task_func)
480-
self.model.tool_choice = "required" # type: ignore
490+
481491
elif self.mode == "coordinate":
482492
_built_in_tools.append(
483493
self.get_transfer_task_function(
@@ -489,7 +499,6 @@ def run(
489499
files=files, # type: ignore
490500
)
491501
)
492-
self.model.tool_choice = "auto" # type: ignore
493502

494503
if self.enable_agentic_context:
495504
_built_in_tools.append(self.set_team_context)
@@ -503,7 +512,6 @@ def run(
503512
files=files, # type: ignore
504513
)
505514
_built_in_tools.append(run_member_agents_func)
506-
self.model.tool_choice = "auto" # type: ignore
507515

508516
if self.enable_agentic_context:
509517
_built_in_tools.append(self.set_team_context)
@@ -3141,6 +3149,10 @@ def _configure_model(self, show_tool_calls: bool = False) -> None:
31413149
# Set show_tool_calls on the Model
31423150
self.model.show_tool_calls = show_tool_calls
31433151

3152+
# Set tool_choice on the Model
3153+
if self.tool_choice is not None:
3154+
self.model.tool_choice = self.tool_choice
3155+
31443156
def _add_tools_to_model(self, model: Model, tools: List[Union[Function, Callable]]) -> None:
31453157
# We have to reset for every run, because we will have new images/audio/video to attach
31463158
self._functions_for_model = {}

0 commit comments

Comments
 (0)