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


##########
providers/common/ai/src/airflow/providers/common/ai/policies/retry.py:
##########
@@ -320,29 +320,58 @@ def _hook(self) -> PydanticAIHook:
 
         return PydanticAIHook.get_hook(self.llm_conn_id, 
hook_params={"model_id": self.model_id})
 
-    def _prompt(self, exception: BaseException, try_number: int, max_tries: 
int) -> str:
-        if self.include_traceback:
-            text = "".join(traceback.format_exception(exception)).rstrip("\n")
-        else:
-            text = str(exception)
+    def _limit(self, text: str, *, keep_tail: bool = False) -> str:
         # Redact before truncating -- truncating first could cut a registered 
secret in half.
         if self.redactor is not None:
             text = self.redactor(text)
-        if len(text) > self.max_exception_length:
-            if self.include_traceback:
-                # Keep the tail: the innermost frames and the final exception 
line say the most.
-                text = f"(truncated) ...{text[-self.max_exception_length :]}"
-            else:
-                text = f"{text[: self.max_exception_length]}... (truncated)"
-        if not self.include_traceback:
-            text = f"{type(exception).__name__}: {text}"
+        if len(text) <= self.max_exception_length:
+            return text
+        if keep_tail:
+            # Keep the tail: the innermost frames and the final exception line 
say the most.
+            return f"(truncated) ...{text[-self.max_exception_length :]}"
+        return f"{text[: self.max_exception_length]}... (truncated)"
+
+    def _prompt(self, exception: BaseException, try_number: int, max_tries: 
int) -> str:
+        if self.include_traceback:
+            text = 
self._limit("".join(traceback.format_exception(exception)).rstrip("\n"), 
keep_tail=True)
+        else:
+            text = f"{type(exception).__name__}: {self._limit(str(exception))}"
+            # Each member gets its own limit: one long error must not push the 
last one, which
+            # decides ``fallback_rules``, out of the prompt.
+            member_errors = self._get_member_errors(exception)
+            for position, member in enumerate(member_errors, start=1):
+                text += (
+                    f"\nchain member {position} of {len(member_errors)}: "
+                    f"{type(member).__name__}: {self._limit(str(member))}"
+                )
+        if (progress := self._describe_journal_progress(exception)) is not 
None:
+            text += f"\n{progress}"
         # ``max_tries`` is the last try number that may still retry (core adds 
to it on a clear),
         # so the final attempt is ``max_tries + 1``, as core logs "Starting 
attempt N of M".
         return (
             "Classify this error from a data pipeline task "
             f"(attempt {try_number} of {max_tries + 1}):\n\n{text}"
         )
 
+    @staticmethod
+    def _get_member_errors(exception: BaseException) -> tuple[BaseException, 
...]:
+        """Return the error each fallback connection raised, in chain order, 
or ``()`` for any other error."""
+        from pydantic_ai.exceptions import FallbackExceptionGroup

Review Comment:
   yes, they should go to the top, updated



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