MCPcopy
hub / github.com/openai/openai-agents-python / _on_invoke_tool_impl

Function _on_invoke_tool_impl

src/agents/tool.py:1978–2013  ·  view source on GitHub ↗
(ctx: ToolContext[Any], input: str)

Source from the content-addressed store, hash-verified

1976 )
1977
1978 async def _on_invoke_tool_impl(ctx: ToolContext[Any], input: str) -> Any:
1979 tool_name = ctx.tool_name
1980 json_data = _parse_function_tool_json_input(tool_name=tool_name, input_json=input)
1981 _log_function_tool_invocation(tool_name=tool_name, input_json=input)
1982
1983 try:
1984 parsed = (
1985 schema.params_pydantic_model(**json_data)
1986 if json_data
1987 else schema.params_pydantic_model()
1988 )
1989 except ValidationError as e:
1990 raise ModelBehaviorError(f"Invalid JSON input for tool {tool_name}: {e}") from e
1991
1992 args, kwargs_dict = schema.to_call_args(parsed)
1993
1994 if not _debug.DONT_LOG_TOOL_DATA:
1995 logger.debug(f"Tool call args: {args}, kwargs: {kwargs_dict}")
1996
1997 if not is_sync_function_tool:
1998 if schema.takes_context:
1999 result = await the_func(ctx, *args, **kwargs_dict)
2000 else:
2001 result = await the_func(*args, **kwargs_dict)
2002 else:
2003 if schema.takes_context:
2004 result = await asyncio.to_thread(the_func, ctx, *args, **kwargs_dict)
2005 else:
2006 result = await asyncio.to_thread(the_func, *args, **kwargs_dict)
2007
2008 if _debug.DONT_LOG_TOOL_DATA:
2009 logger.debug(f"Tool {tool_name} completed.")
2010 else:
2011 logger.debug(f"Tool {tool_name} returned {result}")
2012
2013 return result
2014
2015 function_tool = _build_wrapped_function_tool(
2016 name=schema.name,

Callers

nothing calls this directly

Calls 4

ModelBehaviorErrorClass · 0.85
to_call_argsMethod · 0.80

Tested by

no test coverage detected