Lee-W commented on code in PR #70184:
URL: https://github.com/apache/airflow/pull/70184#discussion_r3622885796
##########
providers/common/ai/src/airflow/providers/common/ai/operators/llm.py:
##########
@@ -154,6 +164,25 @@ def llm_hook(self) -> PydanticAIHook:
}
return PydanticAIHook.get_hook(self.llm_conn_id,
hook_params=hook_params)
+ def _finalize_output(self, output: Any) -> Any:
+ if self._serialize_model_output and isinstance(output, BaseModel):
+ return output.model_dump()
+ return output
+
+ def _defer_llm_call(self) -> None:
Review Comment:
why do we need to make it a separate method
##########
providers/common/ai/src/airflow/providers/common/ai/triggers/llm.py:
##########
@@ -0,0 +1,112 @@
+# 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 collections.abc import AsyncIterator
+from typing import Any
+
+from airflow.providers.common.ai.hooks.pydantic_ai import PydanticAIHook
+from airflow.providers.common.ai.utils.logging import log_run_summary
+from airflow.providers.common.ai.utils.output_type import (
+ deserialize_output_type,
+ serialize_llm_output,
+)
+from airflow.triggers.base import BaseTrigger, TriggerEvent
+
+
+def serialize_usage_limits(usage_limits: Any | None) -> dict[str, Any] | None:
+ """Serialize pydantic-ai ``UsageLimits`` for trigger persistence."""
+ if usage_limits is None:
+ return None
+ return usage_limits.model_dump(exclude_defaults=True)
+
+
+def deserialize_usage_limits(data: dict[str, Any] | None) -> Any | None:
+ """Restore pydantic-ai ``UsageLimits`` from trigger persistence."""
+ if data is None:
+ return None
+ from pydantic_ai.usage import UsageLimits
Review Comment:
why do we need to do inline import here
##########
providers/common/ai/src/airflow/providers/common/ai/operators/llm.py:
##########
@@ -173,17 +206,31 @@ def execute(self, context: Context) -> Any:
if self.require_approval:
self.defer_for_approval(context, output) # type: ignore[misc]
- if self._serialize_model_output and isinstance(output, BaseModel):
- # ``serialize_output=True``, or a core without the worker-side
- # deserialization-class walk: dump to a dict so XCom carries a
plain
- # JSON payload that deserializes without an allow-list entry.
- output = output.model_dump()
+ return self._finalize_output(output)
- return output
+ def execute_complete(
+ self,
+ context: Context,
+ event: dict[str, Any],
+ generated_output: str | None = None,
+ ) -> Any:
+ """Resume after deferrable LLM execution or human review."""
+ if generated_output is not None:
+ output = super().execute_complete(context, generated_output, event)
+ return rehydrate_pydantic_output(
+ self.output_type, output,
serialize_output=self._serialize_model_output
+ )
- def execute_complete(self, context: Context, generated_output: str, event:
dict[str, Any]) -> Any:
- """Resume after human review and restore the Pydantic model for XCom
consumers."""
- output = super().execute_complete(context, generated_output, event)
- return rehydrate_pydantic_output(
- self.output_type, output,
serialize_output=self._serialize_model_output
+ if event.get("status") == "error":
+ raise AirflowException(event.get("message", "LLM call failed in
deferrable mode"))
Review Comment:
We decided not to raise AirflowException directly
https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#don-t-raise-airflowexception-directly
--
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]