Skip to content

Conversation

@kthui
Copy link
Contributor

@kthui kthui commented Sep 3, 2025

Overview:

PyContext should be named Context in Python.

Details:

Before:

/workspace/examples/runtime/hello_world$ python hello_world.py
Traceback (most recent call last):
  File "/workspace/examples/runtime/hello_world/hello_world.py", line 21, in <module>
    from dynamo.runtime import PyContext, DistributedRuntime, dynamo_endpoint, dynamo_worker
ImportError: cannot import name 'PyContext' from 'dynamo.runtime' (/opt/dynamo/venv/lib/python3.12/site-packages/dynamo/runtime/__init__.py). Did you mean: 'Context'?

After:

/workspace/examples/runtime/hello_world$ python hello_world.py
2025-09-03T17:34:28.864169Z  INFO hello_world.worker: [backend] Created service hello_world/backend   
2025-09-03T17:34:28.864216Z  INFO hello_world.worker: [backend] Serving endpoint generate on lease 7587889230630619401

Where should the reviewer start?

N/A

Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)

N/A

Summary by CodeRabbit

  • New Features

    • Hello World example now streams output incrementally instead of returning a single response.
    • Supports cancelling/stop requests mid-response to end streaming gracefully.
  • Behavior Changes

    • Responses are emitted per word with short delays, improving perceived responsiveness for longer inputs.

@kthui kthui self-assigned this Sep 3, 2025
@github-actions github-actions bot added the fix label Sep 3, 2025
@kthui kthui force-pushed the jacky-hello-world-fix branch from 9114491 to 91c5423 Compare September 3, 2025 17:31
@copy-pr-bot
Copy link

copy-pr-bot bot commented Sep 3, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@kthui kthui marked this pull request as ready for review September 3, 2025 17:35
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 3, 2025

Walkthrough

The hello_world runtime example updates its import and function signature to use Context instead of PyContext, converts content_generator into an async generator that streams per-word greetings with 1-second delays, and adds cancellation checks (is_stopped/is_killed) after each delay.

Changes

Cohort / File(s) Summary
Hello world runtime example
examples/runtime/hello_world/hello_world.py
Replace PyContext with Context in imports and function signature; convert content_generator into an async generator that yields per-word "Hello {word}!" with 1s delay; add cancellation checks (is_stopped/is_killed) after sleep; existing logging retained.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor Client
    participant Endpoint as dynamo_endpoint
    participant Worker as dynamo_worker
    participant Gen as content_generator
    participant Ctx as Context

    Client->>Endpoint: HTTP request (text)
    Endpoint->>Worker: Invoke handler(request)
    Worker->>Gen: Start async generator(request, Ctx)
    loop for each word in request
        Gen->>Gen: await sleep(1s)
        Gen->>Ctx: check is_stopped()/is_killed()
        alt Cancellation detected
            Gen-->>Worker: return (stop streaming)
            Note right of Gen: Prints cancellation message
        else Continue
            Gen-->>Worker: yield "Hello {word}!"
            Worker-->>Endpoint: stream chunk
            Endpoint-->>Client: send chunk
        end
    end
    Worker-->>Endpoint: complete
    Endpoint-->>Client: end of stream
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I twitch my ears at each hello I send,
One hop per word, a greeting without end.
If winds cry “stop,” I pause mid-beat—
Streams go quiet, soft and sweet.
Then off I scamper, code left neat. 🐇✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
examples/runtime/hello_world/hello_world.py (1)

29-36: Minor polish: early-cancel check, trim tokens, use logger over print, and add return type.
Keeps behavior identical while improving UX and consistency.

Apply this diff:

-async def content_generator(request: str, context: Context):
+async def content_generator(request: str, context: Context) -> "AsyncGenerator[str, None]":
@@
-    for word in request.split(","):
-        await asyncio.sleep(1)
-        if context.is_stopped() or context.is_killed():
-            print("request got cancelled.")
-            return
-        yield f"Hello {word}!"
+    for word in (w.strip() for w in request.split(",") if w.strip()):
+        # Skip unnecessary sleep if already cancelled
+        if context.is_stopped() or context.is_killed():
+            logger.info("Request cancelled; id=%s", context.id())
+            return
+        await asyncio.sleep(1)
+        if context.is_stopped() or context.is_killed():
+            logger.info("Request cancelled; id=%s", context.id())
+            return
+        yield f"Hello {word}!"

Add this import near the top:

from typing import AsyncGenerator
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 5c197b3 and 91c5423.

📒 Files selected for processing (1)
  • examples/runtime/hello_world/hello_world.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
examples/runtime/hello_world/hello_world.py (2)
lib/bindings/python/src/dynamo/_core.pyi (1)
  • DistributedRuntime (31-54)
lib/bindings/python/src/dynamo/runtime/__init__.py (2)
  • dynamo_endpoint (65-97)
  • dynamo_worker (36-62)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build and Test - dynamo
🔇 Additional comments (2)
examples/runtime/hello_world/hello_world.py (2)

28-29: Double-check response_model type compatibility with dynamo_endpoint.
Decorator signature expects response_model: Type[BaseModel]; passing str may break once response validation is implemented. If strings are intended, confirm the API supports this long-term or switch the example to a simple Pydantic model.


21-21: Verify Context API and stale references

  • No leftover PyContext imports or usage detected in the codebase.
  • Confirm that Context from dynamo._core provides the methods id(), is_stopped(), and is_killed().

@kthui kthui requested a review from keivenchang September 3, 2025 17:43
Copy link
Contributor

@nealvaidya nealvaidya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kthui kthui merged commit 8d54eb7 into main Sep 3, 2025
10 checks passed
@kthui kthui deleted the jacky-hello-world-fix branch September 3, 2025 18:17
kthui added a commit that referenced this pull request Sep 3, 2025
nnshah1 pushed a commit that referenced this pull request Sep 8, 2025
Signed-off-by: nnshah1 <neelays@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants