Skip to content

Add request preprocessing and response postprocessing hooks to TriviaAgentLoop#5

Closed
andreisavu wants to merge 2 commits intomainfrom
claude/add-request-response-processing-jyPIq
Closed

Add request preprocessing and response postprocessing hooks to TriviaAgentLoop#5
andreisavu wants to merge 2 commits intomainfrom
claude/add-request-response-processing-jyPIq

Conversation

@andreisavu
Copy link
Member

Summary

This PR adds extensibility hooks to TriviaAgentLoop that allow subclasses to transform requests before agent processing and responses after agent execution. This enables custom validation, normalization, formatting, and enrichment logic without modifying the core loop implementation.

Key Changes

  • Added preprocess_request() method: Allows subclasses to transform incoming TriviaRequest objects before they are processed by the agent. Default implementation returns the request unchanged.
  • Added postprocess_response() method: Allows subclasses to transform outgoing TriviaResponse objects after agent execution. Default implementation returns the response unchanged.
  • Overridden execute() method: Implements the preprocessing/postprocessing pipeline by:
    • Calling preprocess_request() on the incoming request
    • Delegating to parent MainLoop.execute() with the preprocessed request
    • Calling postprocess_response() on the response output (if present)
    • Safely handling cases where response output is None
  • Updated imports: Added Mapping from collections.abc, and Budget, PromptResponse, and Heartbeat from weakincentives
  • Enhanced documentation: Updated class docstring to document the new preprocessing/postprocessing capabilities and added notes to the prepare() method explaining the execution flow

Implementation Details

  • The preprocessing occurs in execute() before prepare() is called, ensuring consistent request handling throughout the execution flow
  • Postprocessing only applies when prompt_response.output is not None, preventing unnecessary processing of error responses
  • Both hooks use the standard override pattern, making them easy to extend in subclasses
  • Comprehensive test coverage includes default behavior, custom implementations, and edge cases

Testing

Added three new test classes with 8 test cases covering:

  • Default behavior (requests/responses returned unchanged)
  • Custom subclass implementations
  • Integration with the execute() method
  • Edge case handling (None output)

Add two overridable methods for transforming requests before agent
processing and responses before returning to callers:

- preprocess_request(request) - override to transform incoming requests
- postprocess_response(response) - override to transform outgoing responses

Both methods return their input unchanged by default. Subclass TriviaAgentLoop
and override these methods to implement custom preprocessing/postprocessing logic.
- preprocess_request now receives Session for context-aware preprocessing
- postprocess_response now receives both Prompt and Session
- Preprocessing moved inside prepare() for proper request transformation
- Use instance variables to pass context from prepare() to postprocess_response()
@andreisavu andreisavu closed this Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants