Add code mode (Monty sandbox) to common.ai AgentOperator#68407
Draft
kaxil wants to merge 2 commits into
Draft
Conversation
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.
Summary
Adds an opt-in
code_mode=Trueflag toAgentOperator(and@task.agent) that wraps the agent's tools in a singlerun_codetool executed in pydantic-ai's Monty sandbox, via the new[code-mode]extra (pydantic-ai-harness[codemode]). For multi-tool workflows the model writes one Python snippet that calls the tools as functions -- with loops, conditionals, andasyncio.gather-- instead of one model round-trip per tool call, cutting round-trips and token use.Design rationale
pydantic-montyis pre-1.0 and fast-moving (a Rust/native wheel). Pinning it as a hard dependency of every common.ai install would let its churn or any platform-wheel gap break the whole provider for users who never touch code mode. It is gated behind thecode-modeextra and raisesAirflowOptionalProviderFeatureExceptionwhen used without it -- the same pattern the provider already uses formcp,sql, andskills.boolflag rather than passing the capability throughagent_params. Capability instances aren't round-trip-safe through DAG serialization (see the existing "Capabilities" docs note).code_modeis a plain boolean; theCodeModecapability is constructed at execution time in_build_agent, never stored on the serialized operator.run_codetool; the generated code runs deny-by-default (no filesystem, network, or env access) and can only call those tools, which still execute in the worker. Code mode changes how the model invokes tools, not what it can reach.HookToolsetandSQLToolsetnow setreturn_schemaon their tool definitions so code mode renders-> strinstead of-> Any. Both always return serialized strings (_serialize_for_llm/json.dumps), so{"type": "string"}is accurate. The kwarg is applied through a small version-guarded helper becauseToolDefinition.return_schemais newer than the provider's pydantic-ai floor.Usage
Gotchas / limitations
durable=True: durable replay caches individual model/tool steps via a shared step counter that assumes a stable call order across runs, which code mode's free-form generated Python does not guarantee. The combination is rejected at construction (mirroring the existingdurable+enable_hitl_reviewguard).CodeModeround-trip is exercised via a local breeze spike (the harness isn't in CI), and the unit tests cover the provider-owned wiring (build-or-raise, capability injection) with the harness mocked.