[Serve] Call serve.run with a config file path
Description
I would like to have the option to invoke ray.serve.run() with a file path to a Serve config file passed as an argument. For example:
from ray import serve
serve.run(config="config/rayserve.yml")
Right now I can only do this using the subprocess module. For example:
import subprocess
try:
subprocess.check_call(["serve", "run", "config/rayserve.yml", "--blocking"])
except subprocess.CalledProcessError:
...
Alternatively, one could configure all of their deployments using the serve.deployment() decorator arguments and then invoke ray.serve.run() as-is. I'm trying to avoid this because I want all of my configuration to be in the same place (my configuration files).
Use case
The reason I would like to have this feature is that I am implementing dev scripts using the [project.scripts] block in my pyproject.toml configuration file. One of the scripts I'd like to implement is for running the Ray Serve application locally (ie in a development environment on either a laptop or in an Anyscale Workspace).
@zcin any update on this feature request? This would be helpful for my use case as well.
I also need it.
@zcin can I take on this? I'd like to contribute. Thanks
Hi @johntaylor-cell, I noticed you unassigned this on Sep 25, and I'd be interested in picking it up. I couldn't find an open PR for it, so before starting implementation, I'd like to align on the API design.
CC @zcin, since you originally triaged this issue, in case you have additional context on the intended design.
Current behavior
Currently, serve run <config.yaml> in python/ray/serve/scripts.py:
- Loads the config file and validates it against
ServeDeploySchema. - Starts Serve using the config's
proxy_location,http_options,grpc_options,controller_options, and tracing configuration. - Calls
client.deploy_apps(config)to deploy the applications.
There isn't currently an equivalent Python API for this workflow. serve.run only accepts a single Application created using .bind().
Concern with serve.run(config=...)
A Serve config file can define multiple applications, whereas serve.run returns a single DeploymentHandle for one application.
Also, serve.run is decorated with @PublicAPI(stability="stable"), so I'd prefer not to overload its existing signature without maintainer agreement.
Proposed approach
My proposal is to:
- Extract the config loading, validation, Serve startup, and deployment logic from the CLI into a shared internal helper. This would allow both the CLI and the new Python API to use the same code path.
- Introduce a small public API, for example,
serve.run_config(path, blocking=False), that returns deployment handles keyed by application name.
This would keep the existing serve.run API unchanged while providing a Python equivalent of serve run <config.yaml>.
Questions before I start
- API design: Would a new public function like
serve.run_config()be acceptable, or would you prefer adding aconfig=argument toserve.run? - Return value: Should the function return a dictionary of deployment handles keyed by application name, or would you prefer a different return type?
- Config-level options: Should it apply config-level
http_options,grpc_options, andproxy_location, matching the existing CLI behavior? - Input types: Should it also support an already-parsed dictionary or a
ServeDeploySchemainstance, in addition to a file path?
If this direction sounds good, I'm happy to open a PR with tests under python/ray/serve/tests/, along with updates to the API reference and production guide.
For transparency, I'm using an AI assistant to help navigate the codebase and draft the proposal, but I'll review and test everything myself before opening a PR.
Thanks!
Thanks @gamidirohan, please go ahead.
- Add a new
serve.run_config(...)marked@PublicAPI(stability="alpha"), and leaveserve.runas it is. - Return
Dict[str, DeploymentHandle]keyed by app name once every app isRUNNING. Raise onDEPLOY_FAILED, likeserve.rundoes. - Yes, match the CLI.
- Accept a path, a dict or a
ServeDeploySchema.
It deploys declaratively, like serve deploy: apps from an earlier config that aren't in the new one get deleted. Please document and test that.