Lee-W commented on code in PR #76801:
URL: https://github.com/apache/airflow/pull/76801#discussion_r4615784155


##########
providers/common/ai/src/airflow/providers/common/ai/durable/member_stamp.py:
##########
@@ -0,0 +1,83 @@
+# 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.
+"""Record which member of a ``FallbackModel`` chain answered a model 
request."""
+
+from __future__ import annotations
+
+from collections.abc import AsyncIterator
+from contextlib import asynccontextmanager
+from typing import TYPE_CHECKING, Any
+
+from pydantic_ai.models.wrapper import WrapperModel
+
+if TYPE_CHECKING:
+    from pydantic_ai.messages import ModelMessage, ModelResponse
+    from pydantic_ai.models import ModelRequestParameters, StreamedResponse
+    from pydantic_ai.settings import ModelSettings
+    from pydantic_ai.tools import RunContext
+
+MEMBER_STAMP_KEY = "__airflow_common_ai__"
+MEMBER_MODEL_ID_KEY = "member_model_id"
+
+
+def get_member_model_id(response: ModelResponse) -> str | None:
+    """Return the ``model_id`` of the chain member that produced ``response``, 
if it was stamped."""
+    stamp = (response.metadata or {}).get(MEMBER_STAMP_KEY)
+    if not isinstance(stamp, dict):
+        return None
+    member_model_id = stamp.get(MEMBER_MODEL_ID_KEY)
+    return member_model_id if isinstance(member_model_id, str) else None
+
+
+class MemberStampingModel(WrapperModel):
+    """
+    Stamp each response with the ``model_id`` of the wrapped model.
+
+    pydantic-ai's ``FallbackModel`` only records which member answered on 
suspended responses,
+    so a durable replay cannot otherwise tell whether the recorded answer came 
from a model
+    that is still in the chain. The stamp lives under its own key rather than 
the
+    ``__pydantic_ai__`` namespace that pydantic-ai owns.
+    """
+
+    def _stamp(self, response: ModelResponse | StreamedResponse) -> None:
+        if response.metadata is None:
+            response.metadata = {}
+        response.metadata[MEMBER_STAMP_KEY] = {MEMBER_MODEL_ID_KEY: 
self.wrapped.model_id}

Review Comment:
   Done. The hook stamps the connection id next to `model_id` and replay 
matches the pair, so a step answered by a removed gateway connection no longer 
replays.
   
   The caveat is replaced, not gone. A connection is matched by its name, so 
renaming one runs its steps live while changing its host or credentials does 
not. And when two connections serve the same model, pydantic-ai resumes a 
suspended response on the first of them, because it looks the member up by 
`model_id`.



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