br413 commented on code in PR #70184:
URL: https://github.com/apache/airflow/pull/70184#discussion_r3623026162


##########
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:
   Agreed — those helpers did not carry enough logic to justify the 
indirection. I inlined the defer call in execute() and the output serialization 
at each return path in 061ad47.



##########
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:
   Thanks for the pointer to the contributing guide. I added 
LLMOperatorException under providers/common/ai/exceptions.py (same pattern as 
OpenAIBatchJobException) and use that in execute_complete instead of raising 
AirflowException directly. Updated in 061ad47.



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