Skip to content

[serve] add result(...) to DeploymentResponseGenerator to fix static typing#58522

Merged
abrarsheikh merged 3 commits intoray-project:masterfrom
SolitaryThinker:will/serve_typing_generator
Dec 11, 2025
Merged

[serve] add result(...) to DeploymentResponseGenerator to fix static typing#58522
abrarsheikh merged 3 commits intoray-project:masterfrom
SolitaryThinker:will/serve_typing_generator

Conversation

@SolitaryThinker
Copy link
Contributor

Description

This PR adds a dummy .result() API to DeploymentResponseGenerator`.

DeploymentResponseGenerator currently doesn't support .result(), however when calling .remote() on a DeploymentHandle, the return type is a Union of DeploymentResponseGenerator and DeploymentResponse. Meaning if .result() is on the object, a typing error is raised.

Example error without this PR:
image

Related issues

fixes #52493

Additional information

Optional: Add implementation details, API changes, usage examples, screenshots, etc.

@SolitaryThinker SolitaryThinker requested a review from a team as a code owner November 11, 2025 01:07
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses a static typing issue by adding a .result() method to DeploymentResponseGenerator. The implementation, which raises a TypeError at runtime, is a good approach to guide users to the correct API usage for streaming responses. The change is logical and well-implemented. My main feedback is the lack of tests for this new behavior. I've added a comment with a suggestion for a test case to ensure this behavior is maintained and prevent future regressions.

Comment on lines +643 to +662
def result(
self,
*,
timeout_s: Optional[float] = None,
_skip_asyncio_check: bool = False,
) -> Any:
"""This method is not available on DeploymentResponseGenerator.

DeploymentResponseGenerator is returned when using streaming handles.
To get results, iterate over the generator using `for` (outside deployments)
or `async for` (inside deployments) instead of calling `.result()`.

Raises:
TypeError: Always raises, as this method is not supported for generators.
"""
raise TypeError(
"`DeploymentResponseGenerator` doesn't support `.result()`. "
"Use iteration instead: `for item in response` (outside deployments) "
"or `async for item in response` (inside deployments)."
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This is a good fix for the static typing issue. However, the change should be accompanied by a test case to ensure that calling .result() on a DeploymentResponseGenerator raises a TypeError with the expected message. This will prevent future regressions.

You could add a test in python/ray/serve/tests/test_handle_streaming.py within TestAppHandleStreaming, for example:

def test_result_on_generator_raises_error(self, serve_instance, deployment: Deployment):
    h = serve.run(deployment.bind()).options(stream=True)
    gen = h.remote(5)

    with pytest.raises(TypeError, match="doesn't support `.result()`"):
        gen.result()

@ray-gardener ray-gardener bot added the serve Ray Serve Related Issue label Nov 11, 2025
@abrarsheikh abrarsheikh added the go add ONLY when ready to merge, run all tests label Nov 12, 2025
@github-actions
Copy link

github-actions bot commented Dec 6, 2025

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Dec 6, 2025
@abrarsheikh
Copy link
Contributor

please fix the premerge

@SolitaryThinker SolitaryThinker force-pushed the will/serve_typing_generator branch from cf28c60 to ce0a877 Compare December 8, 2025 22:15
@SolitaryThinker
Copy link
Contributor Author

looking into docs error

@github-actions github-actions bot added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Dec 9, 2025
Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: will.lin <will.lin@anyscale.com>
@SolitaryThinker SolitaryThinker force-pushed the will/serve_typing_generator branch from ce0a877 to 9eb64cb Compare December 10, 2025 23:57
@abrarsheikh abrarsheikh merged commit 99b3dae into ray-project:master Dec 11, 2025
6 checks passed
peterxcli pushed a commit to peterxcli/ray that referenced this pull request Feb 25, 2026
…typing (ray-project#58522)

## Description
This PR adds a dummy `.result()` API to DeploymentResponseGenerator`.

`DeploymentResponseGenerator` currently doesn't support `.result()`,
however when calling `.remote()` on a DeploymentHandle, the return type
is a Union of `DeploymentResponseGenerator` and `DeploymentResponse`.
Meaning if `.result()` is on the object, a typing error is raised.

Example error without this PR:
<img width="1838" height="1286" alt="image"
src="https://github.com/user-attachments/assets/8f6a0b48-2926-4fa0-ad2f-1bd411ff7362"
/>

## Related issues
fixes ray-project#52493
## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: will.lin <will.lin@anyscale.com>
Signed-off-by: peterxcli <peterxcli@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[serve] Cannot access attribute "result" for class "DeploymentResponseGenerator" Attribute "result" is unknown

2 participants