kaxil commented on code in PR #76999:
URL: https://github.com/apache/airflow/pull/76999#discussion_r4580725628
##########
providers/common/ai/src/airflow/providers/common/ai/mixins/cancellable_run.py:
##########
@@ -50,10 +54,25 @@ class CancellableAgentRunMixin:
def run_agent_sync(
self, agent: Agent[Any, Any], user_prompt: Any, **run_kwargs: Any
) -> AgentRunResult[Any]:
- """Call ``agent.run_sync`` under a fresh cancellation token held for
:meth:`on_kill`."""
+ """
+ Call ``agent.run_sync`` under a fresh cancellation token held for
:meth:`on_kill`.
+
+ A ``FallbackExceptionGroup`` from a failed chain is raised again as
+
:class:`~airflow.providers.common.ai.fallback_exceptions.FallbackChainExhaustedError`,
carrying
+ what the durable journal replayed and recorded so far.
+ """
self._cancellation_token = CancellationToken()
try:
return agent.run_sync(user_prompt,
cancellation_token=self._cancellation_token, **run_kwargs)
+ except FallbackExceptionGroup as group:
+ if isinstance(group, FallbackChainExhaustedError):
+ raise
+ journal = current_journal()
Review Comment:
Doesn't `current_journal()` return a journal for every running task,
creating one on first use? It only returns `None` outside a task, so
`replayed_steps=0` / `recorded_steps=0` would be set for non-durable runs too,
and the classifier prompt then says "a retry resumes from there" when the model
calls actually run again. Could this read the journal only when durability is
on, or read the active one without creating it? The non-durable test runs
outside a task context, so a case with a real task context and `durable=False`
would cover it.
##########
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:
These two new function-body imports (this one and the one in
`_describe_journal_progress`) don't say why they are lazy. If it is to keep
this module importable without pydantic-ai, a one-line comment saying so would
do. Otherwise they can go to the top of the file.
--
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]