@@ -25,9 +25,10 @@ Code mode
2525 Experimental: this can change or be removed in a minor release of this provider.
2626 See :ref: `howto/stability `.
2727
28- Set ``code_mode=True `` to collapse the agent's tools into a single ``run_code ``
29- tool powered by the `Monty <https://github.com/pydantic/monty >`__ sandbox (via
30- pydantic-ai-harness). Instead of one model round-trip per tool call, the model
28+ Pass pydantic-ai-harness's ``CodeMode `` capability in ``capabilities= `` (see
29+ :ref: `capabilities `) to collapse the
30+ agent's tools into a single ``run_code `` tool powered by the
31+ `Monty <https://github.com/pydantic/monty >`__ sandbox. Instead of one model round-trip per tool call, the model
3132writes a single Python snippet that calls the tools as functions -- with loops,
3233conditionals, and ``asyncio.gather `` -- in one turn. For multi-tool workflows
3334this cuts round-trips and token use.
@@ -68,24 +69,39 @@ mode:
6869 genuinely required rather than by default. See :doc: `sandbox/index ` for the
6970 backends and their limitations.
7071
71- The two are not exclusive: ``code_mode=True `` and a ``SandboxToolset `` can be
72+ The two are not exclusive: ``CodeMode `` and a ``SandboxToolset `` can be
7273enabled together, and the file tools fold into ``run_code `` while ``run_command ``
7374stays a tool of its own.
7475
7576Requires the ``code-mode `` extra::
7677
7778 pip install "apache-airflow-providers-common-ai[code-mode]"
7879
80+ and the import ``from pydantic_ai_harness import CodeMode `` in the Dag file.
81+
7982.. exampleinclude :: /../../ai/src/airflow/providers/common/ai/example_dags/example_agent.py
8083 :language: python
8184 :start-after: [START howto_operator_agent_code_mode]
8285 :end-before: [END howto_operator_agent_code_mode]
8386
84- ``code_mode=True `` is the same as adding ``CodeMode() `` at the end of ``capabilities= `` (see
85- :ref: `capabilities `), except that the operator builds the capability when the task runs, so the
86- Dag file does not import ``pydantic_ai_harness ``.
87+ ``CodeMode `` takes its own arguments; see the
88+ `pydantic-ai-harness code mode docs <https://ai.pydantic.dev/harness/code-mode/ >`__. The one
89+ you are most likely to need is ``max_tool_calls ``: each ``run_code `` snippet may make at most
90+ that many nested tool calls, 100 by default. A snippet that asks for more fails, the calls it
91+ already made are not undone, and the model has to split the work across several snippets. If the
92+ agent fans out over a list, for example one tool call per row or per file, set it above the
93+ largest list you expect.
94+
95+ Code mode cannot be combined with ``durable=True `` (see :doc: `capabilities ` for which
96+ capabilities durable replay covers), and it turns off :doc: `tool_approval `. A tool that needs
97+ approval and is called from ``run_code `` does not run: the model gets an error back and may
98+ carry on without it, so the task can still succeed. To keep such a tool out of ``run_code ``,
99+ list the other tools in ``CodeMode(tools=[...]) ``; called directly, it fails the task.
100+
101+ Importing ``CodeMode `` in the Dag file loads pydantic-ai-harness and Monty each time the file is
102+ parsed. Keep code mode agents in their own Dag file if the rest of the file does not need them.
87103
88104.. note ::
89105
90- Monty is pre-1.0. The ``code-mode `` extra is opt-in so its dependency churn
106+ pydantic-ai-harness is pre-1.0. The ``code-mode `` extra is opt-in so its dependency churn
91107 never affects the base provider install.
0 commit comments