Added example of yielding multiple each item from the list as well as the list itself

This commit is contained in:
SwiftyOS
2025-03-02 17:05:55 +01:00
committed by Swifty
parent 8dabe6c70d
commit 3e117aac5d

View File

@@ -21,8 +21,8 @@ class ExampleBlock(Block):
name: str = SchemaField(
description="The name of the example block", placeholder="Enter a name"
)
greeting: str = SchemaField(
description="The greeting to display", placeholder="Hello"
greetings: list[str] = SchemaField(
description="The greetings to display", default=["Hello", "Hi", "Hey"]
)
is_funny: bool = SchemaField(
description="Whether the block is funny", placeholder="True", default=True
@@ -36,6 +36,12 @@ class ExampleBlock(Block):
response: dict[str, Any] = SchemaField(
description="The response object generated by the example block."
)
all_responses: list[dict[str, Any]] = SchemaField(
description="All the responses from the example block."
)
greeting_count: int = SchemaField(
description="The number of greetings in the input."
)
error: str = SchemaField(description="The error from the example block")
def __init__(self):
@@ -60,7 +66,7 @@ class ExampleBlock(Block):
# This is an instance of the Input schema with sample values.
test_input={
"name": "Craig",
"greeting": "Hello",
"greetings": ["Hello", "Hi", "Hey"],
"is_funny": True,
"credentials": TEST_CREDENTIALS_INPUT,
},
@@ -68,6 +74,16 @@ class ExampleBlock(Block):
# Each output is a tuple of (output_name, output_data).
test_output=[
("response", {"message": "Hello, world!"}),
("response", {"message": "Hello, world!"}), # We mock the function
("response", {"message": "Hello, world!"}), # We mock the function
(
"all_responses",
[
{"message": "Hello, world!"},
{"message": "Hello, world!"},
{"message": "Hello, world!"},
],
),
],
# Function names on the block implementation to mock on test run.
# Each mock is a dictionary with function names as keys and mock implementations as values.
@@ -102,6 +118,25 @@ class ExampleBlock(Block):
def run(
self, input_data: Input, *, credentials: APIKeyCredentials, **kwargs
) -> BlockOutput:
greeting = ExampleBlock.my_static_method(input_data.greeting)
message = self.my_function_that_can_be_mocked(greeting, credentials)
yield "response", {"message": message}
"""
The run function implements the block's core logic. It processes the input_data
and yields the block's output.
In addition to credentials, the following parameters can be specified:
graph_id: The ID of the graph containing this block.
node_id: The ID of this block's node in the graph.
graph_exec_id: The ID of the current graph execution.
node_exec_id: The ID of the current node execution.
user_id: The ID of the user executing the block.
"""
rtn_all_responses: list[dict[str, Any]] = []
# Here we deomonstrate best practice for blocks that need to yield multiple items.
# We yield each item from the list to allow for operations on each element.
# We also yield the complete list for situations when the full list is needed.
for greeting in input_data.greetings:
full_greeting = ExampleBlock.my_static_method(greeting)
message = self.my_function_that_can_be_mocked(full_greeting, credentials)
rtn_all_responses.append({"message": message})
yield "response", {"message": message}
yield "all_responses", rtn_all_responses
yield "greeting_count", len(input_data.greetings)