-
Notifications
You must be signed in to change notification settings - Fork 0
feat(AIC-3210): support graph().stream() in the Python AI SDK #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2cba708
[AIC-3210] feat(AIC-3210): add graph().stream() with OTel parity and …
jeffdupont b03ceb1
[AIC-3210] fix(AIC-3210): keep stream_route off GraphDefinition and s…
jeffdupont 30428e6
[AIC-3210] fix(AIC-3210): mark a cancelled graph stream as run.cancelled
jeffdupont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """ | ||
| Example: ``graph().stream()`` — the streaming counterpart to ``examples/graph_example.py``. | ||
|
|
||
| Model text goes to stdout; node boundaries go to stderr so stdout stays a clean transcript. | ||
|
|
||
| Like ``examples/streaming.py``, the generator is built inside ``conversation_id`` and iterated | ||
| *outside* it. That is the shape a server produces when it hands a stream to a transport, and it | ||
| is what exercises call-time binding: an async generator body does not run until the first | ||
| ``__anext__``, so both the conversation id and the ``ld.ai.graph`` span's OTel parent have to be | ||
| captured when ``stream()`` is called, not when iteration starts. | ||
|
|
||
| Writes no JSON output file, same as the single-config streaming example. | ||
|
|
||
| Usage (via main.py): | ||
| python main.py graph-streaming <flag-key> "<user input>" | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import sys | ||
|
|
||
| import examples.register # noqa: F401 – side-effect: populate global_registry | ||
| from examples.utils import new_context, new_conversation_id | ||
| from launchdarkly_ai_server import conversation_id, global_registry, graph | ||
|
|
||
|
|
||
| async def run(key: str, user_input: str) -> None: | ||
| conversation = new_conversation_id("graph-streaming-example") | ||
| print(f"[conversation] {conversation}", file=sys.stderr) | ||
|
|
||
| with conversation_id(conversation): | ||
| stream = graph( | ||
| key, | ||
| registry=global_registry, | ||
| ).stream(user_input, new_context(), {"user_id": "user-123"}) | ||
|
|
||
| async for event in stream: | ||
| if event["type"] == "chunk": | ||
| sys.stdout.write(event.get("text", "")) | ||
| sys.stdout.flush() | ||
| elif event["type"] == "node_start": | ||
| print(f"\n[node_start] {event['nodeKey']}", file=sys.stderr) | ||
| elif event["type"] == "node_done": | ||
| print( | ||
| f"\n[node_done] {event['nodeKey']} usage={json.dumps(event.get('usage'))}", | ||
| file=sys.stderr, | ||
| ) | ||
| elif event["type"] == "handoff": | ||
| print( | ||
| f"[handoff] {event['sourceKey']} -> {event['targetKey']}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| # Final event — usage aggregated across nodes, plus graph judge results when configured. | ||
| sys.stdout.write("\n\n") | ||
| print("Usage:", json.dumps(event.get("usage"), indent=2, default=str)) | ||
| if event.get("judgeResults"): | ||
| print( | ||
| "Judge results:", | ||
| json.dumps(event["judgeResults"], indent=2, default=str), | ||
| ) | ||
| sys.stdout.write("\n") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.