kaxil commented on code in PR #69003:
URL: https://github.com/apache/airflow/pull/69003#discussion_r3481874113


##########
providers/anthropic/tests/system/anthropic/example_anthropic_agent.py:
##########
@@ -0,0 +1,65 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from airflow.sdk import dag, task
+
+ANTHROPIC_CONN_ID = "anthropic_default"
+
+
+@dag(schedule=None, catchup=False)
+def anthropic_managed_agent():
+    @task
+    def setup_agent_and_environment() -> dict[str, str]:
+        # One-time setup helper — in production run this once and store the 
IDs, do not
+        # create a fresh agent every DAG run. Here we create them and pass the 
IDs via XCom.
+        from airflow.providers.anthropic.hooks.anthropic import AnthropicHook
+
+        hook = AnthropicHook(conn_id=ANTHROPIC_CONN_ID)
+        agent = hook.create_agent(
+            name="airflow-research-agent",
+            system="You are a concise research assistant.",
+            tools=[{"type": "agent_toolset_20260401"}],
+        )
+        environment = hook.create_environment(name="airflow-agent-env")
+        return {"agent_id": agent.id, "environment_id": environment.id}
+
+    setup = setup_agent_and_environment()
+
+    # [START howto_operator_anthropic_agent_session]
+    from airflow.providers.anthropic.operators.agent import 
AnthropicAgentSessionOperator
+
+    run_agent = AnthropicAgentSessionOperator(
+        task_id="run_agent",
+        conn_id=ANTHROPIC_CONN_ID,
+        agent_id=setup["agent_id"],
+        environment_id=setup["environment_id"],
+        message="Summarize the latest stable Apache Airflow release in two 
sentences.",
+        deferrable=True,
+    )
+    # [END howto_operator_anthropic_agent_session]
+
+    run_agent
+
+
+anthropic_managed_agent()
+
+
+from tests_common.test_utils.system_tests import get_test_run  # noqa: E402
+
+# Needed to run the example DAG with pytest (see: 
contributing-docs/testing/system_tests.rst)

Review Comment:
   Applied in 
[`260a157`](https://github.com/apache/airflow/pull/69003/commits/260a157f944e129f2857c147362e202856041f04).



##########
providers/anthropic/tests/system/anthropic/example_anthropic_batch.py:
##########
@@ -0,0 +1,81 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from typing import Any
+
+from airflow.sdk import dag, task
+
+ANTHROPIC_CONN_ID = "anthropic_default"
+MODEL = "claude-opus-4-8"
+
+POKEMON = ["pikachu", "charmander", "bulbasaur"]
+
+
+@dag(schedule=None, catchup=False)
+def anthropic_batch_messages():
+    @task
+    def build_requests(names: list[str]) -> list[dict[str, Any]]:
+        return [
+            {
+                "custom_id": name,
+                "params": {
+                    "model": MODEL,
+                    "max_tokens": 256,
+                    "messages": [{"role": "user", "content": f"Describe {name} 
in one sentence."}],
+                },
+            }
+            for name in names
+        ]
+
+    @task
+    def collect_results(batch_id: str) -> dict[str, str]:
+        # Results stream from the API unordered; key them by custom_id. For 
large
+        # batches, persist to object storage instead of returning via XCom.
+        from airflow.providers.anthropic.hooks.anthropic import AnthropicHook
+
+        hook = AnthropicHook(conn_id=ANTHROPIC_CONN_ID)
+        summaries: dict[str, str] = {}
+        for entry in hook.stream_batch_results(batch_id):
+            if entry.result.type == "succeeded":
+                text = next((b.text for b in entry.result.message.content if 
b.type == "text"), "")
+                summaries[entry.custom_id] = text
+        return summaries
+
+    requests = build_requests(POKEMON)
+
+    # [START howto_operator_anthropic_batch]
+    from airflow.providers.anthropic.operators.anthropic import 
AnthropicBatchOperator
+
+    run_batch = AnthropicBatchOperator(
+        task_id="run_batch",
+        conn_id=ANTHROPIC_CONN_ID,
+        requests=requests,
+        deferrable=True,
+    )
+    # [END howto_operator_anthropic_batch]
+
+    collect_results(batch_id=run_batch.output)
+
+
+anthropic_batch_messages()
+
+
+from tests_common.test_utils.system_tests import get_test_run  # noqa: E402
+
+# Needed to run the example DAG with pytest (see: 
contributing-docs/testing/system_tests.rst)

Review Comment:
   Applied in 
[`260a157`](https://github.com/apache/airflow/pull/69003/commits/260a157f944e129f2857c147362e202856041f04).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to