Class FunctionNode<I,O>

java.lang.Object
com.google.adk.workflow.BaseNode
com.google.adk.workflow.FunctionNode<I,O>
All Implemented Interfaces:
Node

@Experimental public final class FunctionNode<I,O> extends BaseNode
A workflow node that runs a Java function on each input, with a type per port; build one with FunctionNodes.of(String, Class, Class, NodeBody) or FunctionNodes.builder(Class, Class). An Object input port takes its input as it is, a Content or Event port reads its own type, a Void port ignores its input, and Jackson reads any other type, so a user message reaches only a Content or Object port. Every output is written in JSON form, so a successor reads the same value whether it comes live or from a stored session. A body that gives its output to a child with useAsOutput outputs the child's output written as O, as if it had returned it.

The body may send events before its output with Context.emit(Event):

  • State: a write made before emit is sent with that event, or with an event another thread sent just before it; a later write is sent with a later event or the output, and reads on the calling thread after emit returns see the body's own writes.
  • Threads: any thread the body uses may call it, and a call waits even if the body returns; a call deadlocks in a ctx.runNode(...) callback such as doOnSuccess, which runs inside the dispatch. A body that calls it cannot run inline, so on a Runner that does, such as Schedulers.trampoline() or a direct executor, set a FunctionNode.Builder.scheduler that does not, such as Schedulers.io(); ADK Kotlin runs bodies on Dispatchers.IO, where none is needed.
  • Events: a missing ID is filled in; long-running tool IDs, a route or a transfer act as on a returned event; a partial event is sent without waiting, not stored, and carries no state writes; the events precede the output unless a useAsOutput child already sent it.
  • Cancellation: a cancel of the node, or an interrupt of the waiting thread, ends the wait with a CancellationException, and the event may still be stored; after a cancel every call throws one and the output is dropped.
  • Resumes: a body rerun after a pause sends its events again. If the turn is canceled after an emitted event was stored, a later resume reruns the body only if the node sets waitForOutput, or runs through ctx.runNode or as the app's root node with rerunOnResume false.

Only the workflow runner runs a FunctionNode; from another node's body, run it with ctx.runNode, since calling runNode(Context, Object) directly fails with an IllegalStateException.

  • Method Details

    • toBuilder

      public FunctionNode.Builder<I,O> toBuilder()
      Returns a builder initialized with this node's properties. A rerunOnResume the node derived from its confirmation is left unset, so the copy derives it from its own.
    • inputSchema

      public Optional<com.google.genai.types.Schema> inputSchema()
      Returns the schema that describes the input port: the one set on the builder or, if none, one inferred from the input type, where an Object, Void or other type with no fixed JSON shape infers none. An edge into an inferred record or POJO fails at build if the producer's output schema lists a key the type does not, while a builder schema checks only the keys it lists and requires. On a Content port a builder schema describes the message text, and an edge is checked against the Content schema instead.
      Specified by:
      inputSchema in interface Node
      Overrides:
      inputSchema in class BaseNode
    • outputSchema

      public Optional<com.google.genai.types.Schema> outputSchema()
      Returns the schema that describes the output port: the one set on the builder or, if none, one inferred from the output type. A returned Event is sent as it is, so an Event output infers none.
      Specified by:
      outputSchema in interface Node
      Overrides:
      outputSchema in class BaseNode
    • runNode

      public io.reactivex.rxjava3.core.Flowable<?> runNode(Context context, @Nullable Object nodeInput)
      Description copied from interface: Node
      Runs the node on nodeInput, the output it receives from its predecessors. The workflow runner calls this method; to run a node from another node's body, call Context.runNode(Node, Object).

      An emitted Event passes through, an emitted RequestInput pauses the workflow until the user answers it, and any other emitted value becomes the node's output (at most one per run); emit nothing for no output, since RxJava forbids null. The wildcard lets an implementation return a Flowable of its own type.