Skip to content

AutoGen adapter for the Python SDK - #130

Merged
prashar32 merged 1 commit into
mainfrom
feat/autogen-adapter
Jun 14, 2026
Merged

AutoGen adapter for the Python SDK#130
prashar32 merged 1 commit into
mainfrom
feat/autogen-adapter

Conversation

@prashar32

Copy link
Copy Markdown
Owner

Adds an AutoGen adapter so an AutoGen agent (or team) can be bound to a governed run with no code change beyond wrapping the model client.

What it does

GovernedChatCompletionClient wraps any AutoGen v0.4+ ChatCompletionClient (the autogen_core.models protocol that autogen-agentchat agents use) and ticks one governed step per model request before delegating to the real client — one model call == one governed step. So a runaway agent is hard-stopped at its loop/time budget. With gate_tools=True, each tool call the model requests (a FunctionCall in the returned CreateResult) routes through the human-approval gate before the agent can run it. Every other protocol method delegates to the wrapped client, so it's a drop-in:

from riskkernel.adapters.autogen import GovernedChatCompletionClient
from autogen_agentchat.agents import AssistantAgent

client = GovernedChatCompletionClient(model_client, run)   # the one integration line
agent = AssistantAgent("assistant", model_client=client)   # otherwise unchanged
await agent.run(task="...")                                 # raises rk.BudgetExceeded at the cap

I picked the model-client wrapper as the hook because it's the cleanest no-app-change surface that meets all three needs at once: it ticks exactly one step per model request, it sees tool-call requests in the result for gating, and AutoGen does not catch model-client exceptions inside a single agent — so a BudgetExceeded raised there propagates out and halts the run.

Propagation (the asymmetry, verified against the real library)

  • A single agent run directly (agent.run() / agent.on_messages()) does not wrap model-client exceptions — the typed BudgetExceeded propagates out unwrapped.
  • Inside a team (RoundRobinGroupChat, SelectorGroupChat, …) the agent's container catches the exception, serializes it (SerializableException), and the team re-raises it to the caller as a plain RuntimeError(str(error)) whose first line reads "BudgetExceeded: run halted: loop_budget_exceeded". The run still halts — it is not swallowed — but the type is lost. governed_run_errors() (a context manager in the same module) wraps a team call and restores the typed BudgetExceeded / ApprovalDenied so callers can except on it as everywhere else.

Both paths were exercised against a real autogen-agentchat / autogen-core 0.7.5 install: the single-agent integration test halts a real runaway AssistantAgent with BudgetExceeded unwrapped, and the team path was confirmed to re-raise the wrapped RuntimeError that governed_run_errors() then converts back.

Supported versions

Targets the actively maintained v0.4+ line: autogen-agentchat / autogen-core >= 0.4, < 1. Not the legacy pyautogen 0.2 register_reply API. autogen is not a dependency of the SDK — the wrapper is duck-typed and imports nothing from autogen, so the SDK installs and imports fine without it. Added an [autogen] optional extra mirroring the existing [langchain] / [crewai] extras.

Included

  • riskkernel/adapters/autogen.py — the wrapper + governed_run_errors() helper.
  • tests/test_autogen.py — 20 stdlib-only unit tests (step ticking on create/create_stream, loop-budget halt surfaced-not-swallowed, tool gating off/on/denied, protocol delegation, the team-error unwrap) + a skip-unless-installed integration test that halts a real runaway agent.
  • examples/autogen/ — a key-free runnable demo (a local looping model client) that a budget halts, with a README.
  • README, CHANGELOG, pyproject.toml updates.

Full suite: python3 -m unittest discover -s tests -t . → all green (integration test skipped unless autogen is installed; passes when it is).

Closes #84

Bind an AutoGen agent to a governed run with no code change beyond wrapping
the model client. GovernedChatCompletionClient wraps any v0.4+
ChatCompletionClient and ticks one governed step per model request before
delegating, so a runaway agent is hard-stopped at its loop/time budget; with
gate_tools=True each tool call the model requests (a FunctionCall in the
result) routes through the approval gate before the agent can run it. Every
other protocol method delegates to the wrapped client, so it's a drop-in.

A single agent run propagates the halt typed (AutoGen doesn't wrap
model-client exceptions there). A team catches the exception and re-raises it
as a plain RuntimeError("BudgetExceeded: ..."), so I added
governed_run_errors() to restore the typed BudgetExceeded/ApprovalDenied
around a team call — verified both paths against autogen-agentchat 0.7.5.

Targets autogen-agentchat / autogen-core >= 0.4, < 1 (lazily handled — the
wrapper is duck-typed and imports nothing from autogen, so the SDK stays
dependency-free). Adds an [autogen] optional extra, a runnable examples/autogen,
README + CHANGELOG entries, and stdlib-only unit tests plus a skip-unless
integration test that halts a real runaway agent.

Closes #84
@prashar32
prashar32 merged commit 664a67b into main Jun 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoGen adapter

1 participant