Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 56 additions & 72 deletions src/google/adk/a2a/agent/_remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
from ..converters.to_adk_event import MOCK_FUNCTION_CALL_FOR_REQUIRED_USER_AUTH
from ..converters.to_adk_event import MOCK_FUNCTION_CALL_FOR_REQUIRED_USER_INPUT
from ..converters.utils import _is_failed_status
from ..converters.utils import A2A_CLIENT_ERROR_CODE
from ..converters.utils import A2A_TASK_FAILED_ERROR_CODE
from ..converters.utils import A2A_TASK_FAILED_ERROR_MESSAGE
from ..logs.log_utils import build_a2a_request_log
Expand Down Expand Up @@ -536,6 +537,39 @@ def _create_task_failure_events(
return error_event, finish_event


def _create_client_error_event(
ctx: InvocationContext,
agent_name: str,
error_message: str,
*,
a2a_request: Any = None,
extra_metadata: Optional[dict[str, Any]] = None,
) -> Event:
"""Creates the error event for a failure of the A2A exchange itself.

This is for failures on the caller's side of the wire: the agent card or the
credential cannot be resolved, the request fails, the stream is cut, or a
response cannot be handled. A task the remote agent reports as failed is a
different case and carries ``A2A_TASK_FAILED_ERROR_CODE``.

Every such event has the same shape: ``error_code``, ``error_message`` and no
content, with the message also under the ``a2a:error`` metadata key and the
request under ``a2a:request`` when one was already built.
"""
metadata: dict[str, Any] = {A2A_METADATA_PREFIX + "error": error_message}
metadata.update(extra_metadata or {})
if a2a_request:
metadata[A2A_METADATA_PREFIX + "request"] = _compat.a2a_to_dict(a2a_request)
return Event(
author=agent_name,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
error_code=A2A_CLIENT_ERROR_CODE,
error_message=error_message,
custom_metadata=metadata,
)


def _add_mock_function_call(event: Event, state: TaskState) -> None:
"""Generates a mock function call for input-required events if applicable."""
if event.content is None:
Expand Down Expand Up @@ -1859,11 +1893,8 @@ async def _handle_a2a_response(
a2a_response.context_id
)
else:
event = Event(
author=self.name,
error_message="Unknown A2A response type",
invocation_id=ctx.invocation_id,
branch=ctx.branch,
event = _create_client_error_event(
ctx, self.name, "Unknown A2A response type"
)
# Filter out thought parts from user-facing response content.
# Intermediate (submitted/working) events have all parts marked as
Expand All @@ -1876,11 +1907,8 @@ async def _handle_a2a_response(
return event
except A2AClientError as e:
logger.error("Failed to handle A2A response: %s", e)
return Event(
author=self.name,
error_message=f"Failed to process A2A response: {e}",
invocation_id=ctx.invocation_id,
branch=ctx.branch,
return _create_client_error_event(
ctx, self.name, f"Failed to process A2A response: {e}"
)

async def _handle_a2a_response_v2(
Expand Down Expand Up @@ -1958,20 +1986,14 @@ async def _handle_a2a_response_v2(
a2a_response.context_id
)
else:
event = Event(
author=self.name,
error_message="Unknown A2A response type",
invocation_id=ctx.invocation_id,
branch=ctx.branch,
event = _create_client_error_event(
ctx, self.name, "Unknown A2A response type"
)
return event
except A2AClientError as e:
logger.error("Failed to handle A2A response: %s", e)
return Event(
author=self.name,
error_message=f"Failed to process A2A response: {e}",
invocation_id=ctx.invocation_id,
branch=ctx.branch,
return _create_client_error_event(
ctx, self.name, f"Failed to process A2A response: {e}"
)

async def _run_async_impl(
Expand All @@ -1991,12 +2013,7 @@ async def _run_async_impl(
except Exception as e: # pylint: disable=broad-except
task_error_message = f"Failed to authenticate remote A2A agent: {e}"
should_release_task_control = True
yield Event(
author=self.name,
error_message=task_error_message,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
)
yield _create_client_error_event(ctx, self.name, task_error_message)
return
if auth_request_event:
# A pause, not a failure: the invocation resumes once the client
Expand All @@ -2009,12 +2026,7 @@ async def _run_async_impl(
except Exception as e:
task_error_message = f"Failed to initialize remote A2A agent: {e}"
should_release_task_control = True
yield Event(
author=self.name,
error_message=task_error_message,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
)
yield _create_client_error_event(ctx, self.name, task_error_message)
return

# Create A2A request for function response or regular message
Expand Down Expand Up @@ -2210,19 +2222,8 @@ async def _run_async_impl(
)
logger.error(task_error_message)
should_release_task_control = True
yield Event(
author=self.name,
error_message=task_error_message,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
custom_metadata=(
{
A2A_METADATA_PREFIX
+ "request": _compat.a2a_to_dict(a2a_request),
}
if a2a_request
else None
),
yield _create_client_error_event(
ctx, self.name, task_error_message, a2a_request=a2a_request
)

except _compat.A2A_HTTP_ERRORS as e:
Expand All @@ -2231,40 +2232,23 @@ async def _run_async_impl(
should_release_task_control = True
logger.error(error_message)
status_code: object = getattr(e, "status_code", None)
custom_metadata: dict[str, Any] = {
A2A_METADATA_PREFIX + "error": error_message,
A2A_METADATA_PREFIX + "status_code": str(status_code),
}
if a2a_request:
custom_metadata[A2A_METADATA_PREFIX + "request"] = (
_compat.a2a_to_dict(a2a_request)
)
yield Event(
author=self.name,
error_message=error_message,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
custom_metadata=custom_metadata,
yield _create_client_error_event(
ctx,
self.name,
error_message,
a2a_request=a2a_request,
extra_metadata={
A2A_METADATA_PREFIX + "status_code": str(status_code)
},
)

except Exception as e:
error_message = f"A2A request failed: {e}"
task_error_message = error_message
should_release_task_control = True
logger.error(error_message)
custom_metadata = {
A2A_METADATA_PREFIX + "error": error_message,
}
if a2a_request:
custom_metadata[A2A_METADATA_PREFIX + "request"] = (
_compat.a2a_to_dict(a2a_request)
)
yield Event(
author=self.name,
error_message=error_message,
invocation_id=ctx.invocation_id,
branch=ctx.branch,
custom_metadata=custom_metadata,
yield _create_client_error_event(
ctx, self.name, error_message, a2a_request=a2a_request
)

finally:
Expand Down
4 changes: 4 additions & 0 deletions src/google/adk/a2a/converters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
ADK_CONTEXT_ID_SEPARATOR = "/"
A2A_TASK_FAILED_ERROR_CODE = "A2A_TASK_FAILED"
A2A_TASK_FAILED_ERROR_MESSAGE = "Remote agent task failed"
# A failure of the A2A exchange itself, on the caller's side: the agent card or
# the credential cannot be resolved, the request fails, the stream is cut. A task
# the remote agent reports as failed carries A2A_TASK_FAILED_ERROR_CODE instead.
A2A_CLIENT_ERROR_CODE = "A2A_CLIENT_ERROR"


def _is_failed_status(status: Any) -> bool:
Expand Down
Loading
Loading