Overview
Handling outputs

The graph result

Python
TypeScript

story = ComputeText(prompt="tell me a story")
summary = ComputeText(prompt=sb.format("summarize: {story}", story=story.future.text))
response = substrate.run(summary)

substrate.run returns a "graph result" mapping node IDs to node outputs:

response.json

{
"node_1": { "text": "output" },
"node_2": { "text": "other output" }
}

The raw response.json can be a useful way to inspect and debug the outputs of a graph.

But typically, you'll use response.get to retrieve the output of a node:

Python
TypeScript

summary_out = response.get(summary)
print(summary_out.text)

Node configuration

Customizing the graph result

When inspecting the graph result, you can name and hide nodes to make the result easier to work with:

  • id: Name a node by setting id to a custom ID.
  • hide: Hide a node's output from the graph result.

Caching and retries

When building out a graph, it can be useful to cache the result of a node as you iterate on subsequent setups:

  • _cache_age: How long to cache the result, in seconds.
  • _cache_keys: A list of specific output keys to cache. Other keys will not be cached.

When running custom code, it can be useful to retry the code automatically (e.g. when using an unreliable service).

  • _max_retries: The max number of retries to attempt.

Storing outputs


# Returns image data
GenerateImage(prompt="your prompt")
# Returns an image URL
GenerateImage(
prompt="your prompt",
store="hosted",
)

Learn more
  • Use store: "hosted" to host the image on Substrate.
  • Use store: "s3://..." to host on your own storage. See External files to learn more.

Without the store parameter:

  • Nodes with image output return base 64-encoded JPEG data

"image_uri": "data:image/jpeg;base64,/9j/4kZJRBAQYABAAD/4QYRXhpZgAA0AKAA1IBEAAAPEAAAAASg..."

  • Nodes with audio output return base 64-encoded WAV data

"audio_uri": "data:audio/wav;base64,/9j/4QSkZJRgABAQEAYAAAD/BYRXhpZgAATU0A1ABIBAAAFAAAASg..."

Shaping outputs

To shape an output, or combine the outputs of multiple nodes into a single response, you can use a Box:

Python
TypeScript

from substrate import Box
# ...
box = Box(
value={
"story": story.future.text,
"summary": summary.future.text,
}
)
res = substrate.run(box)

You can also stream outputs.