AutoGen adapter for the Python SDK - #130
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
GovernedChatCompletionClientwraps any AutoGen v0.4+ChatCompletionClient(theautogen_core.modelsprotocol thatautogen-agentchatagents 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. Withgate_tools=True, each tool call the model requests (aFunctionCallin the returnedCreateResult) 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: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
BudgetExceededraised there propagates out and halts the run.Propagation (the asymmetry, verified against the real library)
agent.run()/agent.on_messages()) does not wrap model-client exceptions — the typedBudgetExceededpropagates out unwrapped.RoundRobinGroupChat,SelectorGroupChat, …) the agent's container catches the exception, serializes it (SerializableException), and the team re-raises it to the caller as a plainRuntimeError(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 typedBudgetExceeded/ApprovalDeniedso callers canexcepton it as everywhere else.Both paths were exercised against a real
autogen-agentchat/autogen-core0.7.5 install: the single-agent integration test halts a real runawayAssistantAgentwithBudgetExceededunwrapped, and the team path was confirmed to re-raise the wrappedRuntimeErrorthatgoverned_run_errors()then converts back.Supported versions
Targets the actively maintained v0.4+ line:
autogen-agentchat/autogen-core>= 0.4, < 1. Not the legacypyautogen0.2register_replyAPI.autogenis 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 oncreate/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.pyproject.tomlupdates.Full suite:
python3 -m unittest discover -s tests -t .→ all green (integration test skipped unless autogen is installed; passes when it is).Closes #84