Rename and Adapt

Reuse the same function in different contexts by renaming inputs, outputs, and the node itself.

Why Rename?

The same logic often applies in different contexts with different naming conventions:

# Same embedding function, different contexts
embed_query = embed.with_inputs(text="query")
embed_document = embed.with_inputs(text="document")

# Same validation, different pipelines
validate_order = validate.with_outputs(result="order_valid")
validate_user = validate.with_outputs(result="user_valid")

Renaming Inputs

Use .with_inputs() to rename input parameters:

from hypergraph import node

@node(output_name="embedding")
def embed(text: str) -> list[float]:
    return embedder.encode(text)

# Original takes "text"
print(embed.inputs)  # ('text',)

# Adapted to take "query"
query_embed = embed.with_inputs(text="query")
print(query_embed.inputs)  # ('query',)

# Adapted to take "document"
doc_embed = embed.with_inputs(text="document")
print(doc_embed.inputs)  # ('document',)

Important: The original node is unchanged. .with_inputs() returns a new node.

Renaming Outputs

Use .with_outputs() to rename output names:

Renaming the Node

Use .with_name() to give the node a new name:

Chaining Renames

All rename methods return new instances, so you can chain them:

Multiple Inputs/Outputs

Rename multiple at once:

Use Case: Same Function in Multiple Roles

Use Case: Adapting Graphs

Graphs can be renamed too when used as nodes:

Error Handling

If you try to rename a non-existent input/output, you get a clear error:

If you renamed and try to use the old name:

Testing Renamed Nodes

The underlying function is the same:

What's Next?

Last updated