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


##########
providers/common/ai/tests/unit/common/ai/batch/test_pydantic_ai_backend.py:
##########
@@ -0,0 +1,1613 @@
+# 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
+
+import asyncio
+import contextlib
+import dataclasses
+import json
+import threading
+import time
+from contextlib import AsyncExitStack
+from datetime import UTC, datetime
+from unittest import mock
+
+import httpx2
+import pytest
+from pydantic import BaseModel, TypeAdapter
+from structlog.typing import FilteringBoundLogger
+
+pytest.importorskip("anthropic")
+pytest.importorskip("openai")
+pytest.importorskip("google.genai")
+
+from anthropic._decoders.jsonl import AsyncJSONLDecoder
+from anthropic.resources.beta.messages.batches import AsyncBatches as 
AnthropicBatches
+from anthropic.types.beta.messages import BetaMessageBatchIndividualResponse
+from google.genai.batches import AsyncBatches
+from google.genai.types import BatchJob, JobState
+from openai.resources.batches import AsyncBatches as OpenAIAsyncBatches
+from openai.types import Batch as OpenAIBatch
+from pydantic_ai.exceptions import ModelHTTPError, UserError
+from pydantic_ai.messages import (
+    ModelMessage,
+    ModelRequest,
+    ModelResponse,
+    SystemPromptPart,
+    TextPart,
+    ToolCallPart,
+    UserPromptPart,
+)
+from pydantic_ai.models._batch_codec import BatchJobError
+from pydantic_ai.models.anthropic import AnthropicModel
+from pydantic_ai.models.batch import Batch, BatchResult, BatchStatus
+from pydantic_ai.models.function import AgentInfo, FunctionModel
+from pydantic_ai.models.google import GoogleModel
+from pydantic_ai.models.openai import OpenAIResponsesModel
+from pydantic_ai.profiles import ModelProfile
+from pydantic_ai.providers.anthropic import AnthropicProvider
+from pydantic_ai.providers.google import GoogleProvider
+from pydantic_ai.providers.openai import OpenAIProvider
+
+from airflow.providers.common.ai.batch import pydantic_ai_backend as 
backend_module
+from airflow.providers.common.ai.batch.output_schema import build_output_spec
+from airflow.providers.common.ai.batch.pydantic_ai_backend import (
+    _RESULT_BUFFER,
+    AnthropicBatchAdapter,
+    GoogleBatchAdapter,
+    OpenAIBatchAdapter,
+    PydanticAIBatchAdapter,
+    _run,
+    _stream,
+)
+from airflow.providers.common.ai.exceptions import (
+    LLMBatchInputError,
+    LLMBatchJobError,
+    LLMBatchLimitExceededError,
+    LLMBatchModelMismatchError,
+    LLMBatchStateReadError,
+)
+from airflow.providers.common.ai.hooks.pydantic_ai import PydanticAIHook
+from airflow.providers.common.compat.sdk import Connection
+
+KEY = "k" * 16
+CLAUDE = "anthropic:claude-sonnet-4-5"
+CLAUDE_OLD = "anthropic:claude-3-5-haiku-latest"
+GEMINI = "google:gemini-2.5-flash"
+GPT = "openai:gpt-5-mini"
+GPT_4 = "openai:gpt-4.1-mini"
+
+
+class Diagnosis(BaseModel):
+    label: str
+    score: float
+
+
+def _prompt_of(messages: list[ModelMessage]) -> str:
+    last = messages[-1]
+    assert isinstance(last, ModelRequest)
+    return str(next(part.content for part in last.parts if isinstance(part, 
UserPromptPart)))
+
+
+class _Recorder:
+    """A ``FunctionModel`` whose function records every call it serves and 
answers from ``reply``."""
+
+    def __init__(self, reply=None):
+        self.calls: list[tuple[list[ModelMessage], AgentInfo]] = []
+        self.reply = reply or (lambda prompt: 
ModelResponse(parts=[TextPart(f"echo:{prompt}")]))
+        self.model = FunctionModel(self._function)
+
+    def _function(self, messages: list[ModelMessage], info: AgentInfo) -> 
ModelResponse:
+        self.calls.append((messages, info))
+        return self.reply(_prompt_of(messages))
+
+
+def _adapter(model, *, cls=AnthropicBatchAdapter, model_id=CLAUDE, 
handle=None, output_spec=None):
+    return cls(
+        model_id=model_id,
+        batch_handle=handle,
+        output_spec=output_spec,
+        model_factory=lambda _model_id: model,
+    )
+
+
+def _submit(adapter, requests, *, spec=None, **kwargs):
+    spec = spec or build_output_spec(str)
+    kwargs.setdefault("system_prompt", "")
+    kwargs.setdefault("max_tokens", 256)
+    return adapter.submit(
+        requests,
+        model=CLAUDE.partition(":")[2],
+        idempotency_key=KEY,
+        input_fingerprint="fp",
+        output_spec=spec,
+        **kwargs,
+    )
+
+
+def _real_encode(adapter, requests, *, spec=None, **kwargs) -> list:
+    """Encode ``requests`` on the adapter's real model class, which sends 
nothing."""
+    spec = spec or build_output_spec(str)
+
+    async def encode():
+        async with AsyncExitStack() as stack:
+            _, _, encoded = await adapter._encode(
+                stack,
+                requests,
+                model=CLAUDE.partition(":")[2],
+                idempotency_key=KEY,
+                output_spec=spec,
+                system_prompt=kwargs.pop("system_prompt", ""),
+                max_tokens=kwargs.pop("max_tokens", 256),
+                request_params=kwargs.pop("request_params", None),
+            )
+            return encoded
+
+    return asyncio.run(encode())
+
+
+def _claude(name: str = "claude-sonnet-4-5") -> AnthropicModel:
+    return AnthropicModel(name, 
provider=AnthropicProvider(api_key="sk-ant-test"))
+
+
+def _gemini(name: str = "gemini-2.5-flash") -> GoogleModel:
+    return GoogleModel(name, provider=GoogleProvider(api_key="gemini-test"))
+
+
+def _gpt(name: str = "gpt-5-mini") -> OpenAIResponsesModel:
+    return OpenAIResponsesModel(name, 
provider=OpenAIProvider(api_key="sk-test"))
+
+
+def _submitted_on(adapter_cls, model, *, model_name: str, provider_name: str) 
-> str:
+    """Return the handle of a batch submitted on ``model``, with the group the 
real model class computes."""
+    submitter = adapter_cls(model_id=f"{provider_name}:{model_name}", 
model_factory=lambda _id: model)
+    group = _real_encode(submitter, [{"prompt": "x"}])[0].group
+    batch = Batch(
+        id="batches/1",
+        model_name=model_name,
+        provider_name=provider_name,
+        group=group,
+        status="in_progress",
+        created_at=datetime(2026, 10, 1, tzinfo=UTC),
+        request_count=1,
+    )
+    return TypeAdapter(Batch).dump_json(batch).decode()
+
+
+class TestExistingBatchesUseTheModelTheyWereSubmittedWith:
+    """pydantic-ai checks a batch's group, which names the Gemini model, on 
every poll, collect and cancel."""
+
+    @pytest.fixture
+    def pro_batch(self):
+        """A Gemini batch submitted on a model other than the task's own 
``gemini-2.5-flash``."""
+        handle = _submitted_on(
+            GoogleBatchAdapter, _gemini("gemini-2.5-pro"), 
model_name="gemini-2.5-pro", provider_name="google"
+        )
+        return GoogleBatchAdapter(
+            model_id=GEMINI,
+            batch_handle=handle,
+            model_factory=lambda model_id: _gemini(model_id.partition(":")[2]),
+        )
+
+    @pytest.fixture
+    def job(self):
+        with mock.patch.object(
+            AsyncBatches,
+            "get",
+            autospec=True,
+            return_value=BatchJob(name="batches/1", 
state=JobState.JOB_STATE_SUCCEEDED),
+        ) as get:
+            yield get
+
+    def test_a_gemini_batch_submitted_on_another_model_is_polled(self, 
pro_batch, job):
+        assert pro_batch.get_batch("batches/1").status == "completed"
+
+    def test_a_gemini_batch_submitted_on_another_model_is_collected(self, 
pro_batch, job):
+        assert list(pro_batch.iter_results("batches/1")) == []
+
+    def test_a_gemini_batch_submitted_on_another_model_is_cancelled(self, 
pro_batch, job):
+        with mock.patch.object(AsyncBatches, "cancel", autospec=True) as 
cancel:
+            pro_batch.cancel_batch("batches/1")
+
+        assert cancel.call_args.kwargs["name"] == "batches/1"
+
+    @pytest.mark.parametrize("job_state", [JobState.JOB_STATE_CANCELLED, 
JobState.JOB_STATE_EXPIRED])
+    def 
test_a_job_that_ended_unrun_without_a_results_file_collects_as_no_results(self, 
pro_batch, job_state):
+        """pydantic-ai's own collect raises its job error for a job with no 
results file; the backend reads it as none."""
+        with mock.patch.object(
+            AsyncBatches, "get", autospec=True, 
return_value=BatchJob(name="batches/1", state=job_state)
+        ):
+            assert list(pro_batch.iter_results("batches/1")) == []
+
+    def test_a_failed_job_without_a_results_file_is_a_job_error(self, 
pro_batch):
+        with mock.patch.object(
+            AsyncBatches,
+            "get",
+            autospec=True,
+            return_value=BatchJob(name="batches/1", 
state=JobState.JOB_STATE_FAILED),
+        ):
+            with pytest.raises(LLMBatchJobError, match="has no results"):
+                list(pro_batch.iter_results("batches/1"))
+
+    @pytest.mark.parametrize("operation", ["get_batch", "iter_results", 
"cancel_batch"])
+    def 
test_every_operation_builds_the_model_named_by_the_handle_under_the_task_provider(self,
 operation):
+        recorder = _Recorder()
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+        batch = TypeAdapter(Batch).validate_json(submitted.handle)
+        batch.model_name = "claude-haiku-4-5"
+        asked: list[str] = []
+
+        def factory(model_id):
+            asked.append(model_id)
+            return recorder.model
+
+        adapter = AnthropicBatchAdapter(
+            model_id=CLAUDE,
+            batch_handle=TypeAdapter(Batch).dump_json(batch).decode(),
+            model_factory=factory,
+        )
+
+        outcome = getattr(adapter, operation)(submitted.batch_id)
+        if operation == "iter_results":
+            # It reads lazily, so it does nothing until it is iterated.
+            list(outcome)
+
+        assert asked == ["anthropic:claude-haiku-4-5"]
+
+    def test_a_batch_recorded_without_a_handle_uses_the_task_model(self):
+        asked: list[str] = []
+
+        def factory(model_id):
+            asked.append(model_id)
+            return _claude()
+
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=factory)
+
+        with mock.patch.object(AnthropicModel, "batch_cancel", autospec=True):
+            adapter.cancel_batch("msgbatch_old")
+
+        assert asked == [CLAUDE]
+
+    def 
test_a_trigger_serialized_without_a_model_id_fails_with_the_cause(self):
+        adapter = AnthropicBatchAdapter(model_factory=lambda _id: _claude())
+
+        with pytest.raises(LLMBatchStateReadError, match="earlier version of 
this provider.*Retry the task"):
+            adapter.get_batch("msgbatch_old")
+
+
+class _NoLegacyAdapter(PydanticAIBatchAdapter):
+    """A provider that has no way to rebuild a handle from a batch id."""
+
+    name = "plain"
+
+
+class TestInMemoryBatchRoundTrip:
+    """The whole lifecycle against pydantic-ai's in-memory batch API."""
+
+    def 
test_submit_returns_the_real_batch_id_and_a_handle_that_round_trips(self):
+        recorder = _Recorder()
+        adapter = _adapter(recorder.model)
+
+        result = _submit(adapter, [{"prompt": "a"}, {"prompt": "b"}])
+
+        batch = TypeAdapter(Batch).validate_json(result.handle)
+        assert batch.id == result.batch_id
+        assert batch.request_count == 2
+        assert TypeAdapter(Batch).dump_json(batch).decode() == result.handle
+
+    def 
test_a_new_adapter_bound_to_the_stored_handle_polls_and_reads_the_batch(self):
+        recorder = _Recorder()
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}, 
{"prompt": "b"}])
+
+        later = _adapter(recorder.model, handle=submitted.handle)
+        state = later.get_batch(submitted.batch_id)
+        rows = sorted(later.iter_results(submitted.batch_id), key=lambda item: 
item.index)
+
+        assert (state.status, state.counts, state.error_message) == 
("completed", None, None)
+        assert [row.custom_id for row in rows] == [f"{KEY}-0", f"{KEY}-1"]
+        assert [later.extract_output(row, build_output_spec(str)).text for row 
in rows] == [
+            "echo:a",
+            "echo:b",
+        ]
+        assert all(row.provider_status == "success" for row in rows)
+        assert all(row.usage is not None and row.model for row in rows)
+
+    def test_the_async_poll_matches_the_sync_one(self):
+        recorder = _Recorder()
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+
+        state = asyncio.run(_adapter(recorder.model, 
handle=submitted.handle).aget_batch(submitted.batch_id))
+
+        assert state.status == "completed"
+
+    def test_cancel_goes_to_the_model_with_the_stored_handle(self):
+        recorder = _Recorder()
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+        adapter = _adapter(recorder.model, handle=submitted.handle)
+
+        with mock.patch.object(
+            type(recorder.model), "batch_cancel", autospec=True, 
side_effect=type(recorder.model).batch_cancel
+        ) as cancel:
+            adapter.cancel_batch(submitted.batch_id)
+            asyncio.run(adapter.acancel_batch(submitted.batch_id))
+
+        assert [call.args[1].id for call in cancel.call_args_list] == 
[submitted.batch_id] * 2
+
+    def test_polling_without_a_recorded_handle_says_how_to_recover(self):
+        adapter = _adapter(_Recorder().model, cls=_NoLegacyAdapter)
+
+        with pytest.raises(LLMBatchJobError, match="retry the task so it 
re-attaches"):
+            adapter.get_batch("msgbatch_old")
+
+    def test_a_batch_that_has_not_ended_cannot_be_read(self):
+        recorder = _Recorder()
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+        adapter = _adapter(recorder.model, handle=submitted.handle)
+        in_progress = TypeAdapter(Batch).validate_json(submitted.handle)
+        in_progress.status = "in_progress"
+
+        with mock.patch.object(type(recorder.model), "batch_status", 
autospec=True, return_value=in_progress):
+            assert adapter.get_batch(submitted.batch_id).status == 
"in_progress"
+            with pytest.raises(LLMBatchJobError, match="has not ended"):
+                list(adapter.iter_results(submitted.batch_id))
+
+    def test_the_system_prompt_and_settings_reach_the_model(self):
+        recorder = _Recorder()
+        _submit(
+            _adapter(recorder.model),
+            [
+                {"prompt": "a"},
+                {"prompt": "b", "system_prompt": "own", "max_tokens": 9, 
"params": {"top_p": 0.5}},
+            ],
+            system_prompt="batch-wide",
+            request_params={"temperature": 0.2},
+        )
+
+        (first_messages, first_info), (second_messages, second_info) = 
recorder.calls
+        system = lambda messages: [
+            part.content for part in messages[0].parts if isinstance(part, 
SystemPromptPart)
+        ]
+        assert system(first_messages) == ["batch-wide"]
+        assert system(second_messages) == ["own"]
+        assert first_info.model_settings["temperature"] == 0.2
+        assert first_info.model_settings["max_tokens"] == 256
+        assert second_info.model_settings["max_tokens"] == 9
+        assert second_info.model_settings["top_p"] == 0.5
+
+    def 
test_structured_output_is_requested_natively_when_the_model_supports_json_schema(self):
+        recorder = _Recorder()
+        recorder.model._profile = 
ModelProfile(supports_json_schema_output=True)
+        _submit(_adapter(recorder.model), [{"prompt": "a"}], 
spec=build_output_spec(Diagnosis))
+
+        _, info = recorder.calls[0]
+        parameters = info.model_request_parameters
+        assert parameters.output_mode == "native"
+        assert parameters.output_object.json_schema["properties"].keys() == 
{"label", "score"}
+        assert info.output_tools == []
+
+    def test_structured_output_falls_back_to_a_forced_output_tool(self):
+        recorder = _Recorder()
+        recorder.model._profile = 
ModelProfile(supports_json_schema_output=False)
+        _submit(_adapter(recorder.model), [{"prompt": "a"}], 
spec=build_output_spec(Diagnosis))
+
+        _, info = recorder.calls[0]
+        assert info.model_request_parameters.output_mode == "tool"
+        assert [tool.name for tool in info.output_tools] == ["Diagnosis"]
+        assert info.allow_text_output is False
+
+    def test_structured_output_arrives_as_a_tool_call_or_as_json_text(self):
+        spec = build_output_spec(Diagnosis)
+        by_prompt = {
+            "tool": ModelResponse(parts=[ToolCallPart("Diagnosis", {"label": 
"a", "score": 1.0})]),
+            "text": ModelResponse(parts=[TextPart('{"label": "b", "score": 
2.0}')]),
+            "none": ModelResponse(parts=[]),
+        }
+        recorder = _Recorder(lambda prompt: by_prompt[prompt])
+        submitted = _submit(
+            _adapter(recorder.model), [{"prompt": "tool"}, {"prompt": "text"}, 
{"prompt": "none"}], spec=spec
+        )
+
+        adapter = _adapter(recorder.model, handle=submitted.handle, 
output_spec=spec)
+        rows = sorted(adapter.iter_results(submitted.batch_id), key=lambda 
item: item.index)
+        extracted = [adapter.extract_output(row, spec) for row in rows]
+
+        assert [(item.kind, item.value, item.text) for item in extracted] == [
+            ("json_value", {"label": "a", "score": 1.0}, None),
+            ("json_text", None, '{"label": "b", "score": 2.0}'),
+            ("absent", None, None),
+        ]
+
+    def test_an_http_error_becomes_an_errored_row_with_its_status(self):
+        def reply(prompt):
+            raise ModelHTTPError(
+                429, "claude", {"error": {"type": "rate_limit_error", 
"message": "slow down"}}
+            )
+
+        recorder = _Recorder(reply)
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+
+        (row,) = _adapter(recorder.model, 
handle=submitted.handle).iter_results(submitted.batch_id)
+
+        assert row.provider_status == "errored"
+        assert row.error["provider_code"] == "rate_limit_error"
+        assert row.error["stage"] == "provider"
+        assert "429" in row.error["message"]
+
+    def 
test_an_unreadable_result_is_skipped_so_the_merge_counts_it_missing(self):
+        def reply(prompt):
+            raise RuntimeError("boom")
+
+        recorder = _Recorder(reply)
+        submitted = _submit(_adapter(recorder.model), [{"prompt": "a"}])
+
+        rows = list(_adapter(recorder.model, 
handle=submitted.handle).iter_results(submitted.batch_id))
+
+        assert rows == []
+
+    def test_a_result_with_a_foreign_custom_id_is_skipped(self):
+        recorder = _Recorder()
+        adapter = _adapter(recorder.model)
+        bad = BatchResult(custom_id="nodash", status="succeeded", body="{}")
+        parameters = adapter._output_parameters(recorder.model, 
build_output_spec(str))
+
+        assert adapter._decode_or_skip(recorder.model, bad, parameters, "b") 
is None
+
+    @pytest.mark.parametrize(
+        ("status", "expected"), [("expired", "expired"), ("canceled", 
"cancelled"), ("errored", "errored")]
+    )
+    def test_unrun_batch_results_keep_their_own_status(self, status, expected):
+        recorder = _Recorder()
+        adapter = _adapter(recorder.model)
+        result = BatchResult(
+            custom_id=f"{KEY}-3",
+            status=status,
+            body=json.dumps({"status_code": 500, "body": None}) if status == 
"errored" else None,
+        )
+
+        row = adapter._decode_one(
+            recorder.model, result, adapter._output_parameters(recorder.model, 
build_output_spec(str))
+        )
+
+        assert (row.index, row.provider_status) == (3, expected)
+        assert row.raw is None
+        assert row.error["stage"] == "provider"
+
+
+class TestSyncEntryPointsFromARunningLoop:
+    def 
test_a_kill_signal_arriving_inside_a_running_loop_can_still_cancel(self):
+        """``on_kill`` can interrupt the thread while ``asyncio.run`` is on 
its stack."""
+
+        async def inside_a_loop():
+            return _run(asyncio.sleep(0, "done"))
+
+        assert asyncio.run(inside_a_loop()) == "done"
+
+    def 
test_an_error_from_the_batch_call_is_not_chained_onto_the_loop_probe(self):
+        async def fails():
+            raise LLMBatchInputError("bad request")
+
+        with pytest.raises(LLMBatchInputError) as raised:
+            _run(fails())
+
+        assert raised.value.__context__ is None
+
+
+class TestRequestSettings:
+    def 
test_typed_keys_become_settings_and_raw_provider_fields_go_through_extra_body(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE)
+
+        settings, dropped = adapter._build_settings(
+            _claude(),
+            {
+                "temperature": 0.2,
+                "top_k": 5,
+                "metadata": {"user_id": "u"},
+                "anthropic_cache_instructions": True,
+            },
+            {"temperature": 0.9, "thinking": {"type": "enabled", 
"budget_tokens": 1024}},
+            max_tokens=77,
+        )
+
+        assert settings == {
+            "temperature": 0.9,
+            "top_k": 5,
+            "anthropic_cache_instructions": True,
+            "max_tokens": 77,
+            "extra_body": {
+                "metadata": {"user_id": "u"},
+                "thinking": {"type": "enabled", "budget_tokens": 1024},
+            },
+        }
+        assert dropped == set()
+
+    def test_an_explicit_extra_body_is_merged_with_the_raw_fields(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE)
+
+        settings, _ = adapter._build_settings(_claude(), {"extra_body": {"a": 
1}, "b": 2}, max_tokens=8)
+
+        assert settings["extra_body"] == {"a": 1, "b": 2}
+
+    def test_keys_the_batch_sets_itself_are_dropped(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE)
+
+        settings, dropped = adapter._build_settings(
+            _claude(),
+            {"model": "other", "messages": [], "max_tokens": 1, "system": "x", 
"extra_body": {"input": 1}},
+            max_tokens=8,
+        )
+
+        assert settings == {"max_tokens": 8}
+        assert dropped == {"model", "messages", "max_tokens", "system", 
"input"}
+
+    def test_the_dropped_keys_are_logged_once_for_the_whole_batch(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with mock.patch.object(backend_module, "log", 
spec=FilteringBoundLogger) as log:
+            _real_encode(
+                adapter,
+                [{"prompt": "a"}, {"prompt": "b", "params": {"model": "x"}}, 
{"prompt": "c"}],
+                request_params={"system": "x"},
+            )
+
+        log.warning.assert_called_once_with(
+            "Ignoring request parameters the batch sets itself", 
keys=["model", "system"]
+        )
+
+    def test_a_typed_setting_the_model_would_silently_drop_is_rejected(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE)
+
+        with pytest.raises(LLMBatchInputError, match="'seed' is not supported 
by AnthropicModel"):
+            adapter._build_settings(_claude(), {"seed": 3}, max_tokens=8)
+
+    def test_gemini_accepts_typed_settings_and_rejects_raw_fields(self):
+        adapter = GoogleBatchAdapter(model_id=GEMINI)
+
+        assert adapter._build_settings(_gemini(), {"temperature": 0.1, 
"top_k": 4}, max_tokens=8)[0] == {
+            "temperature": 0.1,
+            "top_k": 4,
+            "max_tokens": 8,
+        }
+        with pytest.raises(LLMBatchInputError, match="Unknown request 
parameter 'metadata'"):
+            adapter._build_settings(_gemini(), {"metadata": {}}, max_tokens=8)
+
+    def 
test_gemini_rejects_extra_body_because_the_batch_api_has_no_passthrough(self):
+        adapter = GoogleBatchAdapter(model_id=GEMINI)
+
+        with pytest.raises(LLMBatchInputError, match="'extra_body' is not 
supported by GoogleModel"):
+            adapter._build_settings(_gemini(), {"extra_body": {"a": 1}}, 
max_tokens=8)
+
+
+class TestRealModelEncoding:
+    """One real model object per provider, so the library validates what the 
adapter hands it."""
+
+    def 
test_anthropic_requests_carry_the_settings_the_system_prompt_and_raw_fields(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        (encoded,) = _real_encode(
+            adapter,
+            [{"prompt": "hi", "params": {"metadata": {"user_id": "u"}}}],
+            system_prompt="be brief",
+            request_params={"temperature": 0.2},
+        )
+
+        body = json.loads(encoded.body)
+        assert encoded.custom_id == f"{KEY}-0"
+        assert body["model"] == "claude-sonnet-4-5"
+        assert body["max_tokens"] == 256
+        assert body["temperature"] == 0.2
+        assert body["metadata"] == {"user_id": "u"}
+        assert body["messages"][0]["role"] == "user"
+        assert "be brief" in json.dumps(body["system"])
+
+    def test_anthropic_structured_output_sends_the_schema_natively(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        (encoded,) = _real_encode(adapter, [{"prompt": "hi"}], 
spec=build_output_spec(Diagnosis))
+
+        body = json.loads(encoded.body)
+        assert body["output_config"]["format"]["type"] == "json_schema"
+        assert body["output_config"]["format"]["schema"]["properties"].keys() 
== {"label", "score"}
+
+    def 
test_a_model_without_native_json_schema_gets_a_forced_output_tool(self):
+        adapter = AnthropicBatchAdapter(
+            model_id=CLAUDE_OLD, model_factory=lambda _id: 
_claude("claude-3-5-haiku-latest")
+        )
+
+        (encoded,) = _real_encode(adapter, [{"prompt": "hi"}], 
spec=build_output_spec(Diagnosis))
+
+        body = json.loads(encoded.body)
+        assert [tool["name"] for tool in body["tools"]] == ["Diagnosis"]
+        assert body["tool_choice"]["type"] == "any"
+
+    def test_gemini_requests_carry_the_schema_and_the_sampling_settings(self):
+        adapter = GoogleBatchAdapter(
+            model_id=GEMINI, model_factory=lambda _id: _gemini(), 
output_spec=build_output_spec(Diagnosis)
+        )
+
+        (encoded,) = _real_encode(
+            adapter,
+            [{"prompt": "hi"}],
+            spec=build_output_spec(Diagnosis),
+            system_prompt="be brief",
+            request_params={"temperature": 0.3},
+        )
+
+        body = json.loads(encoded.body)
+        config = body["generationConfig"]
+        assert config["responseMimeType"] == "application/json"
+        assert config["responseJsonSchema"]["properties"].keys() == {"label", 
"score"}
+        assert config["temperature"] == 0.3
+        assert config["maxOutputTokens"] == 256
+        assert "be brief" in encoded.body
+
+    def 
test_a_setting_the_batch_api_cannot_honor_is_an_input_error_naming_the_request(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with pytest.raises(LLMBatchInputError, match="Request 1 cannot be sent 
as a batch"):
+            _real_encode(adapter, [{"prompt": "ok"}, {"prompt": "x", "params": 
{"anthropic_speed": "fast"}}])
+
+    def test_validate_requests_encodes_without_sending_anything(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with (
+            mock.patch.object(
+                AnthropicModel, "batch_encode", autospec=True, 
side_effect=AnthropicModel.batch_encode
+            ) as encode,
+            mock.patch.object(AnthropicModel, "batch_create", autospec=True) 
as create,
+        ):
+            adapter.validate_requests(
+                [{"prompt": "hi"}],
+                model="claude-sonnet-4-5",
+                output_spec=build_output_spec(str),
+                idempotency_key=KEY,
+            )
+
+        assert encode.call_count == 1
+        create.assert_not_called()
+
+    def 
test_a_custom_id_that_would_exceed_the_provider_limit_is_rejected_before_encoding(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with pytest.raises(LLMBatchLimitExceededError, match="custom_id"):
+            adapter.validate_requests(
+                [{"prompt": "hi"}],
+                model="claude-sonnet-4-5",
+                output_spec=build_output_spec(str),
+                idempotency_key="k" * 70,
+            )
+
+    def test_submit_sends_every_encoded_request_in_one_batch(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+        created = Batch(
+            id="msgbatch_1",
+            model_name="claude-sonnet-4-5",
+            provider_name="anthropic",
+            group="g",
+            status="in_progress",
+            created_at=datetime(2026, 10, 1, tzinfo=UTC),
+            request_count=2,
+        )
+
+        with mock.patch.object(AnthropicModel, "batch_create", autospec=True, 
return_value=created) as create:
+            result = adapter.submit(
+                [{"prompt": "a"}, {"prompt": "b"}],
+                model="claude-sonnet-4-5",
+                output_spec=build_output_spec(str),
+                idempotency_key=KEY,
+                input_fingerprint="fp",
+            )
+
+        assert [r.custom_id for r in create.call_args.args[1]] == [f"{KEY}-0", 
f"{KEY}-1"]
+        assert result.batch_id == "msgbatch_1"
+
+    def test_the_batch_size_limits_come_from_the_model(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+        model = _claude()
+
+        with (
+            mock.patch.object(type(model), "batch_max_requests", new=1),
+            pytest.raises(LLMBatchLimitExceededError, match="at most 1 
requests.*expand"),
+        ):
+            _real_encode(adapter, [{"prompt": "a"}, {"prompt": "b"}])
+
+        with (
+            mock.patch.object(type(model), "batch_max_bytes", new=10),
+            pytest.raises(LLMBatchLimitExceededError, match="at most 10 
bytes.*expand"),
+        ):
+            _real_encode(adapter, [{"prompt": "a"}])
+
+
+class TestModelGroups:
+    def test_claude_models_share_a_group_so_one_batch_can_mix_them(self):
+        models = {CLAUDE: _claude(), "anthropic:claude-haiku-4-5": 
_claude("claude-haiku-4-5")}
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, 
model_factory=models.__getitem__)
+
+        encoded = _real_encode(
+            adapter, [{"prompt": "a"}, {"prompt": "b", "model": 
"anthropic:claude-haiku-4-5"}]
+        )
+
+        assert {json.loads(item.body)["model"] for item in encoded} == {
+            "claude-sonnet-4-5",
+            "claude-haiku-4-5",
+        }
+        assert len({item.group for item in encoded}) == 1
+
+    def 
test_gemini_models_do_not_share_a_group_so_a_mixed_batch_is_rejected_before_any_call(self):
+        models = {GEMINI: _gemini(), "google:gemini-2.5-pro": 
_gemini("gemini-2.5-pro")}
+        adapter = GoogleBatchAdapter(model_id=GEMINI, 
model_factory=models.__getitem__)
+
+        with pytest.raises(LLMBatchModelMismatchError, match="Request 1 cannot 
share a batch with request 0"):
+            _real_encode(adapter, [{"prompt": "a"}, {"prompt": "b", "model": 
"google:gemini-2.5-pro"}])
+
+    def test_a_per_request_model_from_another_provider_is_rejected(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with pytest.raises(LLMBatchModelMismatchError, match="Request 0"):
+            _real_encode(adapter, [{"prompt": "a", "model": "openai:gpt-5"}])
+
+
+class TestAnthropicLegacyBatches:
+    def test_a_v1_batch_id_is_turned_into_a_handle_the_provider_accepts(self):
+        model = _claude()
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: model)
+
+        batch = asyncio.run(adapter._unrecorded_batch(model, "msgbatch_old"))
+
+        assert (batch.id, batch.status) == ("msgbatch_old", "in_progress")
+        assert batch.group == _real_encode(adapter, [{"prompt": "x"}])[0].group
+        assert 
TypeAdapter(Batch).validate_json(TypeAdapter(Batch).dump_json(batch)) == batch
+
+    def test_a_v1_batch_is_polled_through_the_real_status_call(self):
+        model = _claude()
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: model)
+        ended = Batch(
+            id="msgbatch_old",
+            model_name="claude-sonnet-4-5",
+            provider_name="anthropic",
+            group=_real_encode(adapter, [{"prompt": "x"}])[0].group,
+            status="ended",
+            created_at=datetime(2026, 10, 1, 12, tzinfo=UTC),
+            request_count=3,
+        )
+
+        with mock.patch.object(AnthropicModel, "batch_status", autospec=True, 
return_value=ended) as status:
+            state = adapter.get_batch("msgbatch_old")
+
+        assert state.status == "completed"
+        assert status.call_args.args[1].id == "msgbatch_old"
+        assert status.call_args.args[1].group == ended.group
+
+    def test_a_v1_batch_is_cancelled_through_the_rebuilt_handle(self):
+        model = _claude()
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: model)
+
+        with mock.patch.object(AnthropicModel, "batch_cancel", autospec=True) 
as cancel:
+            adapter.cancel_batch("msgbatch_old")
+
+        assert cancel.call_args.args[1].id == "msgbatch_old"
+
+    def test_a_connection_the_batch_api_cannot_use_fails_loudly(self):
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, model_factory=lambda 
_id: _claude())
+
+        with (
+            mock.patch.object(
+                AnthropicModel,
+                "batch_encode",
+                autospec=True,
+                side_effect=UserError("requires static credentials"),
+            ),
+            pytest.raises(LLMBatchStateReadError, match="msgbatch_old.*static 
credentials"),
+        ):
+            adapter.get_batch("msgbatch_old")
+
+
+def _anthropic_line(index: int, kind: str = "succeeded") -> bytes:
+    """One line of Anthropic's results file, as the API sends it."""
+    results = {
+        "succeeded": {
+            "type": "succeeded",
+            "message": {
+                "id": f"msg_{index}",
+                "type": "message",
+                "role": "assistant",
+                "model": "claude-sonnet-4-5",
+                "content": [{"type": "text", "text": f"answer {index}"}],
+                "stop_reason": "end_turn",
+                "stop_sequence": None,
+                "usage": {"input_tokens": 3, "output_tokens": 2},
+            },
+        },
+        "errored": {
+            "type": "errored",
+            "error": {
+                "type": "error",
+                "error": {"type": "rate_limit_error", "message": "slow down"},
+                "request_id": "req_1",
+            },
+        },
+        "canceled": {"type": "canceled"},
+        "expired": {"type": "expired"},
+    }
+    return (json.dumps({"custom_id": f"{KEY}-{index}", "result": 
results[kind]}) + "\n").encode()
+
+
+def _row_view(row):
+    """Everything a row carries except the response's own timestamp, which 
differs per decode."""
+    return (
+        row.custom_id,
+        row.index,
+        row.provider_status,
+        row.model,
+        row.usage,
+        row.finish_reason,
+        row.error,
+        None if row.raw is None else row.raw.parts,
+    )
+
+
+class TestAnthropicResultsStream:
+    """Anthropic's results file is read one line at a time instead of as one 
list per batch."""
+
+    @pytest.fixture
+    def adapter(self):
+        model = _claude()
+        handle = _submitted_on(
+            AnthropicBatchAdapter, model, model_name="claude-sonnet-4-5", 
provider_name="anthropic"
+        )
+        ended = dataclasses.replace(TypeAdapter(Batch).validate_json(handle), 
status="ended")
+        adapter = AnthropicBatchAdapter(model_id=CLAUDE, batch_handle=handle, 
model_factory=lambda _id: model)
+        with mock.patch.object(AnthropicModel, "batch_status", autospec=True, 
return_value=ended):
+            yield adapter, model, ended
+
+    @staticmethod
+    def _serve(chunks, response=None):
+        """Patch the SDK's results call to return a fresh decoder over 
``chunks()`` each time."""
+        response = response or mock.create_autospec(httpx2.Response, 
instance=True)
+
+        def results(self, message_batch_id, **kwargs):
+            return AsyncJSONLDecoder(
+                raw_iterator=chunks(), 
line_type=BetaMessageBatchIndividualResponse, http_response=response
+            )
+
+        return mock.patch.object(AnthropicBatches, "results", autospec=True, 
side_effect=results)
+
+    def 
test_the_first_row_arrives_before_the_results_file_has_been_read_to_the_end(self,
 adapter):
+        adapter, _, _ = adapter
+
+        async def chunks():
+            yield _anthropic_line(0)
+            raise RuntimeError("the second line has not arrived")
+
+        with self._serve(chunks):
+            rows = adapter.iter_results("msgbatch_1")
+            first = next(rows)
+
+            assert (first.index, first.provider_status) == (0, "success")
+            with pytest.raises(RuntimeError, match="second line"):
+                next(rows)
+
+    def test_the_streamed_rows_match_pydantic_ais_own_collection(self, 
adapter):
+        kinds = ["succeeded", "errored", "canceled", "expired"]
+
+        async def chunks():
+            for index, kind in enumerate(kinds):
+                yield _anthropic_line(index, kind)
+
+        adapter, model, ended = adapter
+        with self._serve(chunks):
+            reference = asyncio.run(model.batch_results(ended))
+            parameters = adapter._output_parameters(model, 
build_output_spec(str))
+            expected = [adapter._decode_one(model, result, parameters) for 
result in reference]
+
+            with mock.patch.object(AnthropicModel, "batch_results", 
autospec=True) as collect_all:
+                rows = list(adapter.iter_results("msgbatch_1"))
+
+        collect_all.assert_not_called()
+        assert sorted(map(_row_view, rows)) == sorted(map(_row_view, expected))
+        assert [(row.index, row.provider_status) for row in sorted(rows, 
key=lambda r: r.index)] == [
+            (0, "success"),
+            (1, "errored"),
+            (2, "cancelled"),
+            (3, "expired"),
+        ]
+
+    def test_closing_early_stops_the_producer_and_the_download(self, adapter):
+        adapter, _, _ = adapter
+        pulled: list[int] = []
+        response = mock.create_autospec(httpx2.Response, instance=True)
+
+        async def chunks():
+            for index in range(_RESULT_BUFFER * 4):
+                pulled.append(index)
+                yield _anthropic_line(index)
+
+        with self._serve(chunks, response):
+            rows = adapter.iter_results("msgbatch_1")
+            next(rows)
+            rows.close()
+
+        response.aclose.assert_awaited_once()
+        assert len(pulled) <= _RESULT_BUFFER + 3
+        assert not [t for t in threading.enumerate() if t.name == 
"llm-batch-results"]
+
+    def test_a_source_error_mid_stream_still_closes_the_http_response(self, 
adapter):
+        adapter, _, _ = adapter
+        response = mock.create_autospec(httpx2.Response, instance=True)
+
+        async def chunks():
+            yield _anthropic_line(0)
+            raise RuntimeError("connection reset")
+
+        with self._serve(chunks, response):
+            rows = adapter.iter_results("msgbatch_1")
+            next(rows)
+            with pytest.raises(RuntimeError, match="connection reset"):
+                next(rows)
+
+        response.aclose.assert_awaited_once()
+        assert not [t for t in threading.enumerate() if t.name == 
"llm-batch-results"]
+
+    def test_rows_are_decoded_on_the_callers_thread(self, adapter):
+        adapter, _, _ = adapter
+        decoded_on: list[threading.Thread] = []
+        decode = adapter._decode_or_skip
+
+        def record(*args, **kwargs):
+            decoded_on.append(threading.current_thread())
+            return decode(*args, **kwargs)
+
+        async def chunks():
+            yield _anthropic_line(0)
+
+        with self._serve(chunks), mock.patch.object(adapter, 
"_decode_or_skip", side_effect=record):
+            list(adapter.iter_results("msgbatch_1"))
+
+        assert decoded_on == [threading.current_thread()]
+
+
+def _helper_threads() -> list[threading.Thread]:
+    return [t for t in threading.enumerate() if t.name == "llm-batch-results"]
+
+
+class TestStream:
+    """The sync bridge over an async generator must never wait on a read that 
has stalled."""
+
+    def 
test_closing_while_the_source_awaits_a_stalled_read_returns_promptly(self):
+        finished = threading.Event()
+
+        async def source():
+            try:
+                yield 1
+                await asyncio.Event().wait()
+            finally:
+                finished.set()
+
+        items = _stream(source())
+        assert next(items) == 1
+        started = time.monotonic()
+        items.close()
+
+        assert time.monotonic() - started < 1
+        assert finished.is_set()
+        assert not _helper_threads()
+
+    @pytest.mark.parametrize("failure", [asyncio.CancelledError(), 
KeyboardInterrupt()])
+    def 
test_a_base_exception_from_the_source_reaches_the_reader_instead_of_hanging_it(self,
 failure):
+        async def source():
+            yield 1
+            raise failure
+
+        items = _stream(source())
+        assert next(items) == 1
+        with pytest.raises(type(failure)):
+            next(items)
+        assert not _helper_threads()
+
+    def 
test_a_producer_thread_that_dies_without_a_message_fails_the_reader(self):
+        async def source():
+            yield 1
+
+        def die(coro):
+            coro.close()
+
+        with mock.patch.object(backend_module.asyncio, "run", side_effect=die):
+            with pytest.raises(RuntimeError, match="stopped without 
finishing"):
+                list(_stream(source()))
+
+
+class _Lines(httpx2.AsyncByteStream):
+    """A file's body served one line per chunk, which can stop arriving after 
``fail_after`` lines."""
+
+    def __init__(self, lines: list[dict], fail_after: int | None = None):
+        self.lines = [(json.dumps(line) + "\n").encode() for line in lines]
+        self.fail_after = fail_after
+        self.pulled = 0
+        self.closed = False
+
+    async def __aiter__(self):
+        for line in self.lines:
+            if self.fail_after is not None and self.pulled >= self.fail_after:
+                raise RuntimeError("the next line has not arrived")
+            self.pulled += 1
+            yield line
+
+    async def aclose(self):
+        self.closed = True
+
+
+def _openai_output(index: int, status_code: int = 200) -> dict:
+    """One line of OpenAI's output file, as the API writes it."""
+    if status_code == 200:
+        body = {
+            "id": f"resp_{index}",
+            "object": "response",
+            "created_at": 1760000000,
+            "model": "gpt-5-mini",
+            "status": "completed",
+            "output": [
+                {
+                    "id": f"msg_{index}",
+                    "type": "message",
+                    "role": "assistant",
+                    "status": "completed",
+                    "content": [{"type": "output_text", "text": f"answer 
{index}", "annotations": []}],
+                }
+            ],
+            "usage": {
+                "input_tokens": 3,
+                "output_tokens": 2,
+                "total_tokens": 5,
+                "input_tokens_details": {"cached_tokens": 0},
+                "output_tokens_details": {"reasoning_tokens": 0},
+            },
+        }
+    else:
+        body = {"error": {"message": "slow down", "type": "rate_limit_error", 
"code": "rate_limit_exceeded"}}
+    return {
+        "id": f"batch_req_{index}",
+        "custom_id": f"{KEY}-{index}",
+        "response": {"status_code": status_code, "request_id": f"req_{index}", 
"body": body},
+        "error": None,
+    }
+
+
+def _openai_error(index: int, code: str) -> dict:
+    """One line of OpenAI's error file for a request that never got a 
response."""
+    return {
+        "id": f"batch_req_{index}",
+        "custom_id": f"{KEY}-{index}",
+        "response": None,
+        "error": {"code": code, "message": f"request {index}: {code}"},
+    }
+
+
+def _openai_input(count: int) -> list[dict]:
+    return [
+        {"custom_id": f"{KEY}-{i}", "method": "POST", "url": "/v1/responses", 
"body": {}}
+        for i in range(count)
+    ]
+
+
+_OPENAI_JOBS = {

Review Comment:
   Good catch, added a test with a `server_error` line from the error file and 
one for a failed job whose errors have no `message`, and your two mutations now 
break them.



-- 
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