AutoGen Integration

Guard AutoGen tool functions with @guard_autogen_tool, GuardedAutoGenTool, and register_guarded_tools().

Overview

The agentguard.integrations.autogen_integration module provides decorators and helpers to wrap AutoGen tool functions with agentguard protection. Supports AutoGen 0.2.x (pyautogen) and 0.4.x / AG2.

The @guard_autogen_tool Decorator

python
from agentguard.integrations.autogen_integration import guard_autogen_tool
from agentguard import GuardConfig

# Without config — uses defaults
@guard_autogen_tool
def search_web(query: str) -> str:
    '''Search the web for information.'''
    return requests.get(f"https://api.example.com?q={{query}}").text

# With config
@guard_autogen_tool(config=GuardConfig(validate_input=True, max_retries=2))
def query_db(sql: str, limit: int = 100) -> list:
    '''Query the database.'''
    return db.execute(sql, limit=limit)

GuardedAutoGenTool Class

python
from agentguard.integrations.autogen_integration import GuardedAutoGenTool
from agentguard import GuardConfig

def search_web(query: str) -> str:
    '''Search the web for information.'''
    return requests.get(f"https://api.example.com?q={{query}}").text

guarded = GuardedAutoGenTool(search_web, config=GuardConfig(max_retries=2))

# Call directly
result = guarded(query="Python tutorials")

# Get a proxy function with original signature (for AutoGen registration)
proxy = guarded.as_function()

Registering with AutoGen Agents

python
import autogen

config = GuardConfig(validate_input=True, max_retries=2, record=True)

# Method 1: register() convenience method
guarded = GuardedAutoGenTool(search_web, config=config)
guarded.register(
    llm_agent=assistant,
    executor_agent=user_proxy,
    description="Search the web for information",
)

# Method 2: Manual registration with proxy
proxy = guarded.as_function()

@assistant.register_for_llm(description="Search the web")
@user_proxy.register_for_execution()
def search_web_tool(query: str) -> str:
    return proxy(query=query)

Bulk Wrapping with register_guarded_tools()

python
from agentguard.integrations.autogen_integration import register_guarded_tools
from agentguard import GuardConfig
import autogen

config = GuardConfig(validate_input=True, max_retries=2, record=True)
assistant = autogen.AssistantAgent("assistant", llm_config=llm_config)
user_proxy = autogen.UserProxyAgent("user_proxy")

# Guard and register all tools in one call
guarded = register_guarded_tools(
    tools=[search_web, query_db, send_email],
    llm_agent=assistant,
    executor_agent=user_proxy,
    config=config,
)
# guarded is a dict: {{"search_web": GuardedAutoGenTool, ...}}

API Summary

Function / ClassDescription
@guard_autogen_toolDecorator to wrap a function with agentguard
GuardedAutoGenTool(func, config)Class wrapper preserving signature for AutoGen
guard_autogen_tools(tools, config)Bulk wrap → dict[str, GuardedAutoGenTool]
register_guarded_tools(tools, llm, exec, config)Guard + register with agent pair
.as_function()Returns proxy with original signature
.register(llm, exec)Register with an AutoGen agent pair
.acall()Async execution
Edit this page on GitHub