fix: passing the locale to create_react_agent (#745)

This commit is contained in:
Willem Jiang
2025-12-06 11:15:11 +08:00
committed by GitHub
parent 21da2630f3
commit 3191e81939
2 changed files with 14 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ def create_agent(
prompt_template: str, prompt_template: str,
pre_model_hook: callable = None, pre_model_hook: callable = None,
interrupt_before_tools: Optional[List[str]] = None, interrupt_before_tools: Optional[List[str]] = None,
locale: str = "en-US",
): ):
"""Factory function to create agents with consistent configuration. """Factory function to create agents with consistent configuration.
@@ -32,6 +33,7 @@ def create_agent(
prompt_template: Name of the prompt template to use prompt_template: Name of the prompt template to use
pre_model_hook: Optional hook to preprocess state before model invocation pre_model_hook: Optional hook to preprocess state before model invocation
interrupt_before_tools: Optional list of tool names to interrupt before execution interrupt_before_tools: Optional list of tool names to interrupt before execution
locale: Language locale for prompt template selection (e.g., en-US, zh-CN)
Returns: Returns:
A configured agent graph A configured agent graph
@@ -62,13 +64,16 @@ def create_agent(
llm_type = AGENT_LLM_MAP.get(agent_type, "basic") llm_type = AGENT_LLM_MAP.get(agent_type, "basic")
logger.debug(f"Agent '{agent_name}' using LLM type: {llm_type}") logger.debug(f"Agent '{agent_name}' using LLM type: {llm_type}")
logger.debug(f"Creating ReAct agent '{agent_name}'") logger.debug(f"Creating ReAct agent '{agent_name}' with locale: {locale}")
# Use closure to capture locale from the workflow state instead of relying on
# agent state.get("locale"), which doesn't have the locale field
# See: https://github.com/bytedance/deer-flow/issues/743
agent = create_react_agent( agent = create_react_agent(
name=agent_name, name=agent_name,
model=get_llm_by_type(llm_type), model=get_llm_by_type(llm_type),
tools=processed_tools, tools=processed_tools,
prompt=lambda state: apply_prompt_template( prompt=lambda state, captured_locale=locale: apply_prompt_template(
prompt_template, state, locale=state.get("locale", "en-US") prompt_template, state, locale=captured_locale
), ),
pre_model_hook=pre_model_hook, pre_model_hook=pre_model_hook,
) )

View File

@@ -1116,6 +1116,10 @@ async def _setup_and_execute_agent_step(
mcp_servers = {} mcp_servers = {}
enabled_tools = {} enabled_tools = {}
# Get locale from workflow state to pass to agent creation
# This fixes issue #743 where locale was not correctly retrieved in agent prompt
locale = state.get("locale", "en-US")
# Extract MCP server configuration for this agent type # Extract MCP server configuration for this agent type
if configurable.mcp_settings: if configurable.mcp_settings:
for server_name, server_config in configurable.mcp_settings["servers"].items(): for server_name, server_config in configurable.mcp_settings["servers"].items():
@@ -1152,6 +1156,7 @@ async def _setup_and_execute_agent_step(
agent_type, agent_type,
pre_model_hook, pre_model_hook,
interrupt_before_tools=configurable.interrupt_before_tools, interrupt_before_tools=configurable.interrupt_before_tools,
locale=locale,
) )
return await _execute_agent_step(state, agent, agent_type, config) return await _execute_agent_step(state, agent, agent_type, config)
else: else:
@@ -1165,6 +1170,7 @@ async def _setup_and_execute_agent_step(
agent_type, agent_type,
pre_model_hook, pre_model_hook,
interrupt_before_tools=configurable.interrupt_before_tools, interrupt_before_tools=configurable.interrupt_before_tools,
locale=locale,
) )
return await _execute_agent_step(state, agent, agent_type, config) return await _execute_agent_step(state, agent, agent_type, config)