Vincent Chéry
07/11/2025, 9:15 AMMarvin
07/11/2025, 9:15 AMMarvin
07/11/2025, 9:16 AMpython
from prefect import flow
@flow
def fetch_stock_data(symbol: str) -> dict:
"""A flow that fetches and processes stock market data."""
# Simulate fetching stock data
stock_data = {
"symbol": symbol,
"price": 150.25,
"volume": 1000000
}
# You could do more processing here
return stock_data
@flow
def analyze_stocks():
# Call the first flow and use its return value
tesla_data = fetch_stock_data("TSLA")
apple_data = fetch_stock_data("AAPL")
# Process the returned data
total_volume = tesla_data["volume"] + apple_data["volume"]
return {
"stocks_analyzed": [tesla_data["symbol"], apple_data["symbol"]],
"total_volume": total_volume
}
# Run the analysis
result = analyze_stocks()
print(result) # Will print the dictionary with analysis results
In this example:
1. The fetch_stock_data
flow returns a dictionary containing stock information
2. The analyze_stocks
flow calls the first flow twice and uses its return values
3. The final result is a new dictionary containing aggregated information
Return values are useful for:
- Passing data between flows
- Storing results in the Prefect backend for later reference
- Chaining flows together where one flow's output becomes another's input
- Creating reports or summaries of flow execution
- Enabling conditional logic based on flow results
Remember that return values from flows are automatically tracked and stored in Prefect's backend, making them available for inspection through the UI or API. This is particularly useful for debugging, monitoring, and analyzing your workflow results over time.
The return value can be any Python object that can be serialized (dictionaries, lists, numbers, strings, etc.). However, if you're working with generator functions, they need to yield results rather than return them directly.Vincent Chéry
07/11/2025, 9:18 AMMarvin
07/11/2025, 9:18 AMMarvin
07/11/2025, 9:19 AMpersist_result=True
on individual tasks
- Using a result_storage
block to specify where results should be stored
- Setting the environment variable PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
Here's an example of configuring task result storage:
python
from prefect import flow, task
from prefect.filesystems import LocalFileSystem
# Create a storage block for results
local_storage = LocalFileSystem(basepath="/path/to/results")
@task(persist_result=True, result_storage=local_storage)
def my_task():
return "This result will be stored"
@flow
def my_flow():
result = my_task()
return result
You can also configure result storage at the flow level, which will apply to all tasks within that flow:
python
@flow(result_storage=local_storage)
def my_flow():
# All task results in this flow will use the specified storage
...
Key Points to Remember:
- Result storage is optional and must be explicitly enabled
- You need to configure a storage block (e.g., LocalFileSystem, S3, Azure, etc.) to persist results
- Stored results can be useful for:
- Debugging
- Result caching
- Sharing data between flow runs
- Audit trails
- Large results should be carefully considered as they can impact storage costs and performance
If you're working with large datasets or sensitive information, you might want to be selective about which task results you persist and where you store them.Vincent Chéry
07/11/2025, 9:21 AMMarvin
07/11/2025, 9:21 AMMarvin
07/11/2025, 9:21 AMpersist_result=True
2. Configure a result_storage
block to specify where to store the results
Example showing both flow and task persistence:
python
from prefect import flow, task
from prefect.filesystems import LocalFileSystem
# Create a storage block
storage = LocalFileSystem(basepath="/path/to/storage")
@task(persist_result=True, result_storage=storage)
def my_task():
return "Task result will be stored"
@flow(persist_result=True, result_storage=storage)
def my_flow():
task_result = my_task()
return "Flow result will be stored"
You can also enable persistence globally using the environment variable PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
, which will affect both flows and tasks.
Thank you for asking for clarification - it's important to be precise about these details!