Class GenerateParams

java.lang.Object
com.google.genkit.ai.middleware.GenerateParams

public class GenerateParams extends Object
Holds parameters for the GenerationMiddleware.wrapGenerate(com.google.genkit.core.ActionContext, com.google.genkit.ai.middleware.GenerateParams, com.google.genkit.ai.middleware.GenerateNext) hook.

The request is a GenerateActionOptions — the high-level generate options that have not yet been resolved to a ModelRequest. This allows wrapGenerate middleware to modify values such as the model name, tool list, or output format before resolution occurs.

This mirrors the JS SDK where the generate middleware hook receives GenerateActionOptions (high-level), while the model middleware hook receives GenerateRequest/ModelRequest (low-level, resolved).

  • Constructor Details

    • GenerateParams

      public GenerateParams(GenerateActionOptions request, int iteration, int messageIndex)
      Creates GenerateParams.
      Parameters:
      request - the current high-level generate options for this iteration
      iteration - the current tool-loop iteration (0-indexed)
      messageIndex - the current message index in the conversation (position the next model response will occupy). This mirrors the JS SDK's messageIndex and is useful for streaming chunk attribution and middleware that tracks conversation position.
    • GenerateParams

      public GenerateParams(GenerateActionOptions request, int iteration, int messageIndex, Consumer<ModelResponseChunk> onChunk)
      Creates GenerateParams with all fields including streaming callback.
      Parameters:
      request - the current high-level generate options for this iteration
      iteration - the current tool-loop iteration (0-indexed)
      messageIndex - the current message index
      onChunk - the streaming chunk callback, or null if not streaming. Mirrors the JS SDK's onChunk in the generate middleware hook, allowing middleware to observe or transform streaming chunks.
    • GenerateParams

      public GenerateParams(GenerateActionOptions request, int iteration)
      Creates GenerateParams with messageIndex defaulting to the message count in the request.
      Parameters:
      request - the current high-level generate options for this iteration
      iteration - the current tool-loop iteration (0-indexed)
  • Method Details

    • getRequest

      public GenerateActionOptions getRequest()
      Returns the current high-level generate options.

      Unlike ModelParams.getRequest(), which returns a resolved ModelRequest, this returns the unresolved GenerateActionOptions containing the model name as a string, tool names as string references, etc.

    • getIteration

      public int getIteration()
      Returns the current tool-loop iteration (0-indexed), equivalent to JS currentTurn.
    • getMessageIndex

      public int getMessageIndex()
      Returns the current message index — the position in the conversation that the next model response will occupy. Starts at 0 and increments as messages are added (model responses, tool responses, etc.). Equivalent to the JS SDK's messageIndex.
    • getOnChunk

      public Consumer<ModelResponseChunk> getOnChunk()
      Returns the streaming chunk callback, or null if this is a non-streaming request.

      Mirrors the JS SDK's onChunk in the generate middleware hook. When non-null, the model will be called with streaming enabled and each chunk will be forwarded through this callback. Middleware can wrap or replace the callback to observe/transform streaming chunks.

    • withRequest

      public GenerateParams withRequest(GenerateActionOptions request)
      Returns a new GenerateParams with the given request, preserving other fields.
    • withMessageIndex

      public GenerateParams withMessageIndex(int messageIndex)
      Returns a new GenerateParams with the given messageIndex, preserving other fields.
    • withOnChunk

      public GenerateParams withOnChunk(Consumer<ModelResponseChunk> onChunk)
      Returns a new GenerateParams with the given onChunk callback, preserving other fields.