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 input port reads its input by type:
  • Void ignores it; Object, or a type the input already has, takes it as is.
  • Any other type rejects an Event and takes a Content message as its non-thought text: the text itself if the port's schema is a string, as for String or an enum (byte[] reads it as base64), and otherwise the text's JSON, or the text if it is not JSON.
  • Jackson converts that value, or any other input, to the port's type.

Every output, including the output of a useAsOutput child, is written as O in JSON form, so a successor reads the same value live or from a stored session.

A schema set on the builder is checked on each run, and the node receives or outputs the value converted to the schema's types: the input before the port reads it, the output after it is written in JSON form. A rejected confirmation's output and a requestInput answer used as the output are not checked; neither is an inferred schema.

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.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Fluent builder for FunctionNode.
  • Method Summary

    Modifier and Type
    Method
    Description
    Optional<com.google.genai.types.Schema>
    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.
    Optional<com.google.genai.types.Schema>
    Returns the schema that describes the output port: the one set on the builder or, if none, one inferred from the output type.
    io.reactivex.rxjava3.core.Flowable<?>
    runNode(Context context, @Nullable Object nodeInput)
    Runs the node on nodeInput, the output it receives from its predecessors.
    Returns a builder initialized with this node's properties.

    Methods inherited from class BaseNode

    config, description, name, rerunOnResume, stateSchema, validateOutgoingEdges, waitForOutput
    Modifier and Type
    Method
    Description
    Returns the node's retry policy and execution timeout.
    Returns what the node does, for humans and for a model that may call it.
    final String
    Returns the name that identifies the node within its graph.
    boolean
    Returns true if a resumed node runs again from scratch, rather than completing with the resuming answer as its output.
    Optional<com.google.genai.types.Schema>
    Returns the schema of the state keys that the node and its children may write; a child inherits it unless it declares its own.
    protected void
    Throws GraphValidationException if this node cannot serve the edges that leave it, such as an edge for a route it never emits.
    boolean
    Returns true if the node stays re-triggerable until it produces an output or a route, instead of completing when its run completes.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Node

    requiresAllPredecessors
    Modifier and Type
    Method
    Description
    default boolean
    Returns true if the node runs only once every predecessor has completed, receiving all their outputs keyed by node name, as a fan-in node does.
  • 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 neither reads nor writes, so a key the type writes under another name than it reads passes and reads as null, 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. On an Event or genai port, or a list, map or array of them, a builder schema does not let the port take keys its type lacks.
      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.