You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2026/07/08 16:29:02 UTC

[PR] Default the Anthropic batch model from the connection [airflow]

kaxil opened a new pull request, #69624:
URL: https://github.com/apache/airflow/pull/69624

   
   `AnthropicBatchOperator` made you repeat `params["model"]` in every request of
   `requests=[{"custom_id", "params"}]`, even though the Anthropic connection already carries a
   `default_model` (`extra['model']`) that the hook's `create_message` / `count_tokens` use.
   Batches were the one path that ignored it.
   
   Now a request that omits `model` inherits one. Resolution order:
   
   1. the request's own `params["model"]` (unchanged; a batch can still mix models)
   2. the operator's new [`model`](https://github.com/astronomer/airflow/blob/babd7fe0240b4b6dfdf8394c6f0527dfc8cb6fd5/providers/anthropic/src/airflow/providers/anthropic/operators/batch.py#L87) argument
   3. the connection's `default_model` (`extra['model']`, else the provider default)
   
   ## Design notes
   
   - This aligns the batch path with the rest of the hook: `create_message` / `count_tokens`
     already resolve `model or self.default_model`. The connection is the natural home for "which
     model", and batches now honor it.
   - `params` stays a verbatim passthrough of the Anthropic `messages.create` payload.
     [`create_batch`](https://github.com/astronomer/airflow/blob/babd7fe0240b4b6dfdf8394c6f0527dfc8cb6fd5/providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py#L425)
     fills a missing `model` via
     [`_apply_default_model`](https://github.com/astronomer/airflow/blob/babd7fe0240b4b6dfdf8394c6f0527dfc8cb6fd5/providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py#L413)
     without mutating the caller's request dicts, and leaves an explicit per-request model
     untouched, so mixed-model batches keep working.
   - Defaulting runs after the first-party guard and deliberately skips the Bedrock-prefix
     `_resolve_model` step: the Message Batches API is first-party + AWS only, so the Bedrock
     branch is unreachable here.
   
   ## Usage
   
   ```python
   # Set the model once for the whole batch instead of on every request:
   AnthropicBatchOperator(
       task_id="summarize",
       model="claude-opus-4-8",  # or omit and set extra['model'] on the connection
       requests=[
           {"custom_id": "t1", "params": {"max_tokens": 1024, "messages": [...]}},
           {"custom_id": "t2", "params": {"max_tokens": 1024, "messages": [...]}},
       ],
   )
   ```
   
   A request that sets its own `model` overrides both, so you can still mix models within one batch.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Default the Anthropic batch model from the connection [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #69624:
URL: https://github.com/apache/airflow/pull/69624#discussion_r3546166323


##########
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py:
##########
@@ -409,17 +409,35 @@ def count_tokens(
             params["system"] = system
         return self.conn.messages.count_tokens(**params).input_tokens
 
-    def create_batch(self, requests: list[dict[str, Any]]) -> MessageBatch:
+    @staticmethod
+    def _apply_default_model(request: dict[str, Any], default_model: str) -> dict[str, Any]:
+        """
+        Fill ``params['model']`` from ``default_model`` when the request omits it.
+
+        The input dict is never mutated, and a request that sets its own ``model`` is
+        returned unchanged, so a single batch can still mix models across requests.
+        """
+        params = request.get("params")
+        if not isinstance(params, dict) or params.get("model"):

Review Comment:
   Yeah, intentional. `params` is the `messages.create` payload, so it also carries the required `messages`/`max_tokens`. If it's missing or `None` the request is already invalid and the batch API rejects it either way, so filling in `params={"model": ...}` wouldn't rescue it, it'd just swap the SDK's error for a more confusing one. I only fill `model` when there's a real `params` dict that omits it, and leave a malformed request untouched so the SDK raises on it.
   
   If you'd prefer we fail faster with a clearer message, I can add an explicit check that raises when a request has no `params` dict. Happy to do that in this PR.
   



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Default the Anthropic batch model from the connection [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #69624:
URL: https://github.com/apache/airflow/pull/69624#discussion_r3545996968


##########
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py:
##########
@@ -409,17 +409,35 @@ def count_tokens(
             params["system"] = system
         return self.conn.messages.count_tokens(**params).input_tokens
 
-    def create_batch(self, requests: list[dict[str, Any]]) -> MessageBatch:
+    @staticmethod
+    def _apply_default_model(request: dict[str, Any], default_model: str) -> dict[str, Any]:
+        """
+        Fill ``params['model']`` from ``default_model`` when the request omits it.
+
+        The input dict is never mutated, and a request that sets its own ``model`` is
+        returned unchanged, so a single batch can still mix models across requests.
+        """
+        params = request.get("params")
+        if not isinstance(params, dict) or params.get("model"):

Review Comment:
   I think there's a small edge case here. If params is None (or missing), not isinstance(params, dict) evaluates to True, so we return immediately and never get a chance to populate the default model. Was that intentional?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Default the Anthropic batch model from the connection [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #69624:
URL: https://github.com/apache/airflow/pull/69624#discussion_r3545996968


##########
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py:
##########
@@ -409,17 +409,35 @@ def count_tokens(
             params["system"] = system
         return self.conn.messages.count_tokens(**params).input_tokens
 
-    def create_batch(self, requests: list[dict[str, Any]]) -> MessageBatch:
+    @staticmethod
+    def _apply_default_model(request: dict[str, Any], default_model: str) -> dict[str, Any]:
+        """
+        Fill ``params['model']`` from ``default_model`` when the request omits it.
+
+        The input dict is never mutated, and a request that sets its own ``model`` is
+        returned unchanged, so a single batch can still mix models across requests.
+        """
+        params = request.get("params")
+        if not isinstance(params, dict) or params.get("model"):

Review Comment:
   ```suggestion
           if not isinstance(params, dict) and params.get("model"):
   ```
   Shouldn't this condition use and instead? With the current or, if params is None, the first condition evaluates to True and returns immediately, so we never get a chance to add the default model. Is that intentional?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Default the Anthropic batch model from the connection [airflow]

Posted by "gopidesupavan (via GitHub)" <gi...@apache.org>.
gopidesupavan commented on code in PR #69624:
URL: https://github.com/apache/airflow/pull/69624#discussion_r3546234420


##########
providers/anthropic/src/airflow/providers/anthropic/hooks/anthropic.py:
##########
@@ -409,17 +409,35 @@ def count_tokens(
             params["system"] = system
         return self.conn.messages.count_tokens(**params).input_tokens
 
-    def create_batch(self, requests: list[dict[str, Any]]) -> MessageBatch:
+    @staticmethod
+    def _apply_default_model(request: dict[str, Any], default_model: str) -> dict[str, Any]:
+        """
+        Fill ``params['model']`` from ``default_model`` when the request omits it.
+
+        The input dict is never mutated, and a request that sets its own ``model`` is
+        returned unchanged, so a single batch can still mix models across requests.
+        """
+        params = request.get("params")
+        if not isinstance(params, dict) or params.get("model"):

Review Comment:
   nah thats fine :) thanks for clarifying..



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Default the Anthropic batch model from the connection [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #69624:
URL: https://github.com/apache/airflow/pull/69624


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org