autogen_core.components.tools#

class autogen_core.components.tools.BaseTool(args_type: Type[ArgsT], return_type: Type[ReturnT], name: str, description: str)[source]#

Bases: ABC, Tool, Generic[ArgsT, ReturnT]

args_type() Type[BaseModel][source]#
property description: str#
load_state_json(state: Mapping[str, Any]) None[source]#
property name: str#
return_type() Type[Any][source]#
return_value_as_string(value: Any) str[source]#
abstract async run(args: ArgsT, cancellation_token: CancellationToken) ReturnT[source]#
async run_json(args: Mapping[str, Any], cancellation_token: CancellationToken) Any[source]#
save_state_json() Mapping[str, Any][source]#
property schema: ToolSchema#
state_type() Type[BaseModel] | None[source]#
class autogen_core.components.tools.BaseToolWithState(args_type: Type[ArgsT], return_type: Type[ReturnT], state_type: Type[StateT], name: str, description: str)[source]#

Bases: BaseTool[ArgsT, ReturnT], ABC, Generic[ArgsT, ReturnT, StateT]

abstract load_state(state: StateT) None[source]#
load_state_json(state: Mapping[str, Any]) None[source]#
abstract save_state() StateT[source]#
save_state_json() Mapping[str, Any][source]#
pydantic model autogen_core.components.tools.CodeExecutionInput[source]#

Bases: BaseModel

Show JSON schema
{
   "title": "CodeExecutionInput",
   "type": "object",
   "properties": {
      "code": {
         "description": "The contents of the Python code block that should be executed",
         "title": "Code",
         "type": "string"
      }
   },
   "required": [
      "code"
   ]
}

Fields:
  • code (str)

field code: str [Required]#

The contents of the Python code block that should be executed

pydantic model autogen_core.components.tools.CodeExecutionResult[source]#

Bases: BaseModel

Show JSON schema
{
   "title": "CodeExecutionResult",
   "type": "object",
   "properties": {
      "success": {
         "title": "Success",
         "type": "boolean"
      },
      "output": {
         "title": "Output",
         "type": "string"
      }
   },
   "required": [
      "success",
      "output"
   ]
}

Fields:
  • output (str)

  • success (bool)

field output: str [Required]#
field success: bool [Required]#
ser_model() str[source]#
class autogen_core.components.tools.FunctionTool(func: Callable[[...], Any], description: str, name: str | None = None)[source]#

Bases: BaseTool[BaseModel, BaseModel]

Create custom tools by wrapping standard Python functions.

FunctionTool offers an interface for executing Python functions either asynchronously or synchronously. Each function must include type annotations for all parameters and its return type. These annotations enable FunctionTool to generate a schema necessary for input validation, serialization, and for informing the LLM about expected parameters. When the LLM prepares a function call, it leverages this schema to generate arguments that align with the function’s specifications.

Note

It is the user’s responsibility to verify that the tool’s output type matches the expected type.

Parameters:
  • func (Callable[..., ReturnT | Awaitable[ReturnT]]) – The function to wrap and expose as a tool.

  • description (str) – A description to inform the model of the function’s purpose, specifying what it does and the context in which it should be called.

  • name (str, optional) – An optional custom name for the tool. Defaults to the function’s original name if not provided.

Example

import random
from autogen_core.base import CancellationToken
from autogen_core.components.tools import FunctionTool
from typing_extensions import Annotated


async def get_stock_price(ticker: str, date: Annotated[str, "Date in YYYY/MM/DD"]) -> float:
    # Simulates a stock price retrieval by returning a random float within a specified range.
    return random.uniform(10, 200)


# Initialize a FunctionTool instance for retrieving stock prices.
stock_price_tool = FunctionTool(get_stock_price, description="Fetch the stock price for a given ticker.")

# Execute the tool with cancellation support.
cancellation_token = CancellationToken()
result = await stock_price_tool.run_json({"ticker": "AAPL", "date": "2021/01/01"}, cancellation_token)

# Output the result as a formatted string.
print(stock_price_tool.return_value_as_string(result))
async run(args: BaseModel, cancellation_token: CancellationToken) Any[source]#
class autogen_core.components.tools.ParametersSchema[source]#

Bases: TypedDict

properties: Dict[str, Any]#
required: NotRequired[Sequence[str]]#
type: str#
class autogen_core.components.tools.PythonCodeExecutionTool(executor: CodeExecutor)[source]#

Bases: BaseTool[CodeExecutionInput, CodeExecutionResult]

async run(args: CodeExecutionInput, cancellation_token: CancellationToken) CodeExecutionResult[source]#
class autogen_core.components.tools.Tool(*args, **kwargs)[source]#

Bases: Protocol

args_type() Type[BaseModel][source]#
property description: str#
load_state_json(state: Mapping[str, Any]) None[source]#
property name: str#
return_type() Type[Any][source]#
return_value_as_string(value: Any) str[source]#
async run_json(args: Mapping[str, Any], cancellation_token: CancellationToken) Any[source]#
save_state_json() Mapping[str, Any][source]#
property schema: ToolSchema#
state_type() Type[BaseModel] | None[source]#
class autogen_core.components.tools.ToolSchema[source]#

Bases: TypedDict

description: NotRequired[str]#
name: str#
parameters: NotRequired[ParametersSchema]#