-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbasic_streaming_pipeline.py
More file actions
63 lines (44 loc) · 2.02 KB
/
basic_streaming_pipeline.py
File metadata and controls
63 lines (44 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) Microsoft. All rights reserved.
"""Basic streaming pipeline using the functional workflow API.
Stream workflow events in real time with run(stream=True).
"""
import asyncio
from agent_framework import workflow
# Plain async functions — no decorators needed for simple helpers.
async def fetch_data(url: str) -> dict[str, str | int]:
"""Simulate fetching data from a URL."""
return {"url": url, "content": f"Data from {url}", "status": 200}
async def transform_data(data: dict[str, str | int]) -> str:
"""Transform raw data into a summary string."""
return f"[{data['status']}] {data['content']}"
async def validate_result(summary: str) -> bool:
"""Validate the transformed result."""
return len(summary) > 0 and "[200]" in summary
# @workflow enables .run(stream=True), which returns a ResponseStream
# you can iterate over with `async for`. Without @workflow, you'd just
# have a normal async function with no streaming capability.
@workflow
async def data_pipeline(url: str) -> str:
"""A simple sequential data pipeline."""
raw = await fetch_data(url)
summary = await transform_data(raw)
is_valid = await validate_result(summary)
return f"{summary} (valid={is_valid})"
async def main():
# run(stream=True) returns a ResponseStream that yields events as they
# are produced. The raw stream includes lifecycle events (started, status)
# alongside application events — filter by event.type to find what you need.
stream = data_pipeline.run("https://example.com/api/data", stream=True)
async for event in stream:
if event.type == "output":
print(f"Output: {event.data}")
# After iteration, get_final_response() returns the WorkflowRunResult
result = await stream.get_final_response()
print(f"Final state: {result.get_final_state()}")
"""
Expected output:
Output: [200] Data from https://example.com/api/data (valid=True)
Final state: WorkflowRunState.IDLE
"""
if __name__ == "__main__":
asyncio.run(main())