kaxil commented on code in PR #76801:
URL: https://github.com/apache/airflow/pull/76801#discussion_r4649631060
##########
providers/common/ai/src/airflow/providers/common/ai/durable/capability.py:
##########
@@ -808,6 +830,37 @@ def _decode_tool_payload(payload: Any) -> Any:
raise DurableJournalError(f"durable execution found a tool result it
cannot replay: {kind!r}")
+def _is_stamped_chain(model: Model) -> TypeGuard[FallbackModel]:
+ return isinstance(model, FallbackModel) and all(
+ isinstance(member, MemberStampingModel) for member in model.models
+ )
+
+
+def _find_unusable_answerer(response: ModelResponse, model: Model) -> str |
None:
+ """Return why a recorded response cannot replay into ``model``'s chain, or
``None`` if it can."""
+ if not _is_stamped_chain(model):
+ return None
+ if (member_model_id := get_member_model_id(response)) is None:
+ return _UNSTAMPED_ANSWERER
+ member_conn_id = get_member_conn_id(response)
+ answerers = {
+ (member.conn_id, member.model_id)
+ for member in model.models
+ if isinstance(member, MemberStampingModel)
Review Comment:
`_is_stamped_chain` has already checked that every member is a
`MemberStampingModel`, so this filter is only there for the type checker. Could
it return the members instead (`_get_stamped_members(model) ->
list[MemberStampingModel] | None`), so both callers get the narrowed type
without a second `isinstance`?
##########
providers/common/ai/tests/unit/common/ai/hooks/test_pydantic_ai.py:
##########
@@ -907,12 +921,55 @@ def test_param_builds_chain_in_order(self, registry,
infer_model_stub):
model = hook.get_conn()
assert isinstance(model, FallbackModel)
- assert model.models == [
+ assert get_chain_members(model) == [
infer_model_stub.models["openai:gpt-5.6-sol"],
infer_model_stub.models["anthropic:claude-opus-4-6"],
infer_model_stub.models["groq:llama-4"],
]
+ @pytest.mark.asyncio
+ async def test_response_is_stamped_with_the_member_that_answered(self,
registry, infer_model_stub):
+ def fail(messages, info):
+ raise ModelAPIError("primary", "down")
+
+ def answer(messages, info):
+ return ModelResponse(parts=[TextPart("answered")])
+
+ infer_model_stub.models["openai:gpt-5.6-sol"] = FunctionModel(fail,
model_name="primary-model")
+ infer_model_stub.models["anthropic:claude-opus-4-6"] = FunctionModel(
+ answer, model_name="second-model"
+ )
+ registry.add("primary", extra={"model": "openai:gpt-5.6-sol"})
+ registry.add("second", extra={"model": "anthropic:claude-opus-4-6"})
+
+ model = PydanticAIHook(llm_conn_id="primary",
fallback_conn_ids=["second"]).get_conn()
+ response = await model.request([ModelRequest.user_text_prompt("go")],
None, ModelRequestParameters())
+
+ assert get_member_model_id(response) == "function:second-model"
+ assert get_member_conn_id(response) == "second"
+
+ @pytest.mark.asyncio
+ async def
test_two_connections_serving_one_model_each_stamp_their_own_conn_id(
+ self, registry, infer_model_stub
+ ):
+ asked = []
+
+ def fail_then_answer(messages, info):
+ asked.append(info)
+ if len(asked) == 1:
+ raise ModelAPIError("shared", "down")
+ return ModelResponse(parts=[TextPart("answered")])
+
+ infer_model_stub.models["openai:gpt-5.6-sol"] =
FunctionModel(fail_then_answer, model_name="shared")
+ registry.add("primary", extra={"model": "openai:gpt-5.6-sol"})
+ registry.add("gateway", extra={"model": "openai:gpt-5.6-sol"})
+
+ model = PydanticAIHook(llm_conn_id="primary",
fallback_conn_ids=["gateway"]).get_conn()
+ response = await model.request([ModelRequest.user_text_prompt("go")],
None, ModelRequestParameters())
+
+ assert get_member_conn_id(response) == "gateway"
Review Comment:
Nothing here checks the primary's `conn_id`: the primary fails, so only the
gateway's stamp gets asserted. With the hook building the primary as
`MemberStampingModel(model, conn_id=None)` the common.ai unit tests still pass,
and a renamed primary connection would then keep replaying its steps. Could
this also assert `[member.conn_id for member in model.models] == ["primary",
"gateway"]`?
##########
providers/common/ai/tests/unit/common/ai/durable/test_fingerprint.py:
##########
@@ -441,6 +441,32 @@ def
test_content_settings_still_count_when_timeout_present(self):
assert high is not None
assert low != high
+ @pytest.mark.skipif(
+ "model_id" not in {field.name for field in
dataclasses.fields(ModelResponse)},
Review Comment:
The floor is now `pydantic-ai-slim>=2.66.0`, which has
`ModelResponse.model_id`, so this skip can't fire. If pydantic-ai later renamed
the field, the test would skip while the `pop("model_id")` in `_strip_volatile`
stopped matching. Can the `skipif` go, so that shows up as a failure?
--
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]