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: |
extract_reasoning_from_events | Extract reflection reasoning from ADK events, preferring thought-tagged parts. TYPE: |
Exported Functions
extract_trajectory: Main extraction API with configuration supportextract_reasoning_from_events: Extract reflection reasoning from event stream
Examples:
Extract reasoning from events:
from gepa_adk.utils.events import extract_reasoning_from_events
reasoning = extract_reasoning_from_events(captured_events)
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_reasoning_from_events ¶
Extract reflection reasoning from ADK event stream.
Extracts thought-tagged parts (part.thought=True) from final response events, preferring actions.response_content over content.parts to avoid duplication (mirrors extract_final_output precedence). Returns None if no events, no final responses, or no thought-tagged text found. Models without thinking support produce None — this is intentional to avoid returning the proposed text as misleading "reasoning".
| PARAMETER | DESCRIPTION |
|---|---|
events | List of ADK Event objects from agent execution, or None. TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
str | None | Extracted reasoning text, or None if no reasoning could be found. |
Source code in src/gepa_adk/utils/events.py
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
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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
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.