Events
events ¶
Trajectory extraction and processing utilities.
This module provides functions for extracting, redacting, and truncating trajectory data from ADK agent execution events.
The main entry point is extract_trajectory, which orchestrates a three-stage pipeline:
- Extract raw data (tool calls, state deltas, token usage)
- Apply redaction to sensitive fields
- Apply truncation to oversized strings
All functions except extract_trajectory are private helpers prefixed with underscore. The extraction logic is designed to gracefully handle both real ADK Event objects and test mocks.
| ATTRIBUTE | DESCRIPTION |
|---|---|
extract_trajectory | Main trajectory extraction API with configurable redaction and truncation. TYPE: |
Exported Functions
extract_trajectory: Main extraction API with configuration support
See Also
gepa_adk.domain.trajectory: Domain models (ADKTrajectory, ToolCallRecord)gepa_adk.domain.types: Configuration (TrajectoryConfig)
Note
These utilities handle infrastructure concerns like data transformation and security (redaction), not domain logic. They consume domain models but don't define them.
extract_final_output ¶
Extract final output text from ADK event stream.
Extracts the final response text from ADK events, handling both event.actions.response_content (preferred) and event.content.parts (fallback) response sources. Filters out reasoning/thought content marked with part.thought=True.
| PARAMETER | DESCRIPTION |
|---|---|
events | List of ADK Event objects from agent execution. TYPE: |
prefer_concatenated | If True, concatenate all non-thought text parts from all final response events. If False (default), return only the last non-thought text part from the last final response event. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | Extracted output text as a string. Returns empty string if no valid |
str | output can be extracted (empty events, no final responses, all thought |
str | parts, or missing attributes). |
Examples:
Basic extraction (default mode - returns LAST final response):
Streaming/concatenation mode for CriticScorer:
events = await runner.run_async(...)
output = extract_final_output(events, prefer_concatenated=True)
Note
Scans events for is_final_response()=True, filters thought parts, skips empty/None text, and handles missing attributes gracefully. Response source priority: response_content > content.parts. Default mode returns LAST final response (for multi-agent pipelines).
Source code in src/gepa_adk/utils/events.py
extract_trajectory ¶
extract_trajectory(
events: list[Any],
final_output: str = "",
error: str | None = None,
config: TrajectoryConfig | None = None,
) -> ADKTrajectory
Extract trajectory from ADK execution events with optional processing.
Extracts tool calls, state deltas, and token usage from ADK event stream, applying redaction and truncation based on configuration.
| PARAMETER | DESCRIPTION |
|---|---|
events | List of ADK Event objects from agent execution. TYPE: |
final_output | Final text response from the agent. Defaults to empty string. TYPE: |
error | Error message if execution failed. Defaults to None. TYPE: |
config | Extraction configuration. If None, uses TrajectoryConfig defaults. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ADKTrajectory | ADKTrajectory with extracted and processed data according to config. |
Examples:
Basic extraction with defaults:
from google.adk.events import Event
events = [...] # From ADK runner
trajectory = extract_trajectory(events, final_output="Response")
Custom configuration:
config = TrajectoryConfig(
include_tool_calls=True,
include_state_deltas=False,
redact_sensitive=True,
max_string_length=5000,
)
trajectory = extract_trajectory(events, config=config)
With error:
Note
Extraction follows this order: 1. Extract raw data from events (tool calls, state, tokens) 2. Apply redaction if config.redact_sensitive is True 3. Apply truncation if config.max_string_length is not None 4. Build immutable ADKTrajectory
Empty events list is valid and returns empty trajectory.
Source code in src/gepa_adk/utils/events.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
extract_output_from_state ¶
Extract output from session state using output_key.
A shared utility for extracting agent output from ADK session state using the output_key mechanism. Complements extract_final_output() for use when agents have output_key configured.
| PARAMETER | DESCRIPTION |
|---|---|
session_state | ADK session state dictionary. TYPE: |
output_key | Key where agent stored its output, or None. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | None | Output string if found in state, None otherwise. |
str | None | Caller should implement fallback logic when None is returned. |
Examples:
Basic extraction:
state = {"proposed_component_text": "Be helpful and concise"}
result = extract_output_from_state(state, "proposed_component_text")
# result == "Be helpful and concise"
Missing key returns None:
state = {"other_key": "value"}
result = extract_output_from_state(state, "proposed_component_text")
# result is None
None output_key returns None:
state = {"proposed_component_text": "text"}
result = extract_output_from_state(state, None)
# result is None
Note
State-based extraction complements event-based extraction for ADK's output_key mechanism. Callers should implement fallback logic (e.g., extract_final_output) when this function returns None.
Source code in src/gepa_adk/utils/events.py
partition_events_by_agent ¶
Partition ADK events by their originating agent.
Separates a mixed event stream (e.g., from SequentialAgent) into per-agent event lists based on the event.author field. Each agent's events can then be processed independently for trajectory extraction.
| PARAMETER | DESCRIPTION |
|---|---|
events | List of ADK Event objects from multi-agent execution. Each event should have an TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, list[Any]] | Dictionary mapping agent names to their respective event lists. |
dict[str, list[Any]] | Events with author='user' or missing author are excluded. |
dict[str, list[Any]] | Empty dict returned if no agent events found. |
Examples:
Basic partitioning from SequentialAgent:
events = await runner.run_async(sequential_agent, ...)
partitions = partition_events_by_agent(events)
# partitions == {
# "generator": [Event(...), Event(...)],
# "critic": [Event(...), Event(...)],
# }
Building per-agent trajectories:
partitions = partition_events_by_agent(events)
trajectories = {}
for agent_name, agent_events in partitions.items():
trajectories[agent_name] = extract_trajectory(agent_events)
Note
Sorts events into agent-specific lists by examining event.author. User events are excluded since they represent input, not agent output.