[serve][llm] Fix serve build YAML serialization for AggregationFunction enum#58509
Conversation
PR ray-project#56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config. This fix adds a helper function that recursively converts Enum objects to their string values before YAML dump, ensuring proper serialization of all enum types in the configuration. Fixes ray-project#58485 Signed-off-by: Nikhil Ghosh <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request addresses an issue with YAML serialization of Enum objects in the serve build command by introducing a recursive helper function to convert enums to strings. The fix is correct and solves the problem.
I've provided a suggestion to use PyYAML's custom representers instead of a manual conversion function. This is a more idiomatic and robust approach that simplifies the code. Overall, good work on fixing this bug.
There was a problem hiding this comment.
Since we are using a dumper, we should probably add the parsing logic to it?
We can use the add_multi_representer and add one for Enum
that should take care of list, dict and other nested objects
Something like this at the end of the file
def enum_representer(dumper, data):
return dumper.represent_scalar("tag:yaml.org,2002:str", data.value)
ServeDeploySchemaDumper.add_multi_representer(Enum, enum_representer)
Move _convert_enums_to_strings helper function before decorator chain to ensure CLI decorators are correctly applied to build() function. Addresses decorator misplacement issue identified in code review. Signed-off-by: Nikhil Ghosh <[email protected]>
|
/gemini review |
Replace manual enum-to-string conversion with PyYAML's add_multi_representer. This is more idiomatic and cleaner, following the pattern used for other custom types (e.g., str_presenter). The representer automatically handles nested structures without manual recursion. Addresses feedback from code review. Signed-off-by: Nikhil Ghosh <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a fix for a RepresenterError that occurs during YAML serialization in the serve build command when an AggregationFunction enum is present in the autoscaling configuration. The fix involves adding a new helper function, _convert_enums_to_strings, which recursively traverses the configuration dictionary and converts any Enum instances to their string values. This function is then applied to the configuration object before it's passed to yaml.dump.
The changes are well-implemented, targeted, and effectively resolve the described issue. The new helper function is clean, correctly handles nested data structures, and is appropriately placed. The overall fix is solid. I have no further comments. Good work!
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a YAML serialization issue with Enum objects in the serve build command by introducing a custom representer. The fix is correct and well-implemented. I have one suggestion to enhance type safety and maintain consistency with the existing codebase.
python/ray/serve/scripts.py
Outdated
| super().write_line_break() | ||
|
|
||
|
|
||
| def enum_representer(dumper, data): |
There was a problem hiding this comment.
Register enum representer with yaml.SafeDumper instead of only ServeDeploySchemaDumper. This ensures enum serialization works for: - serve config command (uses yaml.safe_dump) - serve status command (uses yaml.safe_dump) - serve build command (uses ServeDeploySchemaDumper which extends SafeDumper) Fixes issue where config/status commands would fail with RepresenterError when encountering enum values in autoscaling configs. Signed-off-by: Nikhil Ghosh <[email protected]>
Add type hints to match the pattern used in str_presenter function for better type safety and code consistency. Signed-off-by: Nikhil Ghosh <[email protected]>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a RepresenterError during YAML serialization of AggregationFunction enum values in the serve build command. The fix introduces a custom YAML representer for Enum objects, ensuring they are serialized as their string values. This is achieved by registering the representer with yaml.SafeDumper, which applies the fix to all YAML dumps in serve CLI commands.
The approach is sound. I've added one suggestion to make the new representer more robust by explicitly converting enum values to strings, which will prevent potential issues with enums that have non-string values.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Nikhil G <[email protected]>
addressed, thanks 👍🏽 |
Signed-off-by: Nikhil Ghosh <[email protected]>
|
please add a test for this. Also what does |
2a64295 to
e4f91cf
Compare
@abrarsheikh it creates a YAML string scalar node with the value "mean" for example (which would produce
Added a test to ensure enum representer correctly serializes |
|
|
||
| def test_autoscaling_config_with_enum_serialization(self): | ||
| """Test that AutoscalingConfig with AggregationFunction enum serializes correctly.""" | ||
| from ray.serve.config import AutoscalingConfig |
There was a problem hiding this comment.
move imports to top of file
| yaml_str = yaml.safe_dump(config_dict, sort_keys=False) | ||
| parsed = yaml.safe_load(yaml_str) |
There was a problem hiding this comment.
i think we should be testing the cli build command, not yaml.safe_dump internal implementation
Signed-off-by: Nikhil Ghosh <[email protected]>
e4f91cf to
768e6df
Compare
Signed-off-by: Nikhil Ghosh <[email protected]>
- Fix: Register Enum representer only on ServeDeploySchemaDumper to avoid global side effects - Also updated serve config and serve status to use the custom dumper. Signed-off-by: Nikhil Ghosh <[email protected]>
…on enum (ray-project#58509) PR ray-project#56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config. ex. `yaml.representer.RepresenterError: ('cannot represent an object', <AggregationFunction.MEAN: 'mean'>)` This fix adds 1. a helper function that recursively converts Enum objects to their string values before YAML dump, ensuring proper serialization of all enum types in the configuration. 2. a test that (a) Creates a temporary output YAML file (b) Reads the config from that file (c) Verifies that AggregationFunction.MEAN is correctly serialized as "mean" (string) Fixes ray-project#58485 --------- Signed-off-by: Nikhil Ghosh <[email protected]> Signed-off-by: Nikhil G <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…on enum (ray-project#58509) PR ray-project#56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config. ex. `yaml.representer.RepresenterError: ('cannot represent an object', <AggregationFunction.MEAN: 'mean'>)` This fix adds 1. a helper function that recursively converts Enum objects to their string values before YAML dump, ensuring proper serialization of all enum types in the configuration. 2. a test that (a) Creates a temporary output YAML file (b) Reads the config from that file (c) Verifies that AggregationFunction.MEAN is correctly serialized as "mean" (string) Fixes ray-project#58485 --------- Signed-off-by: Nikhil Ghosh <[email protected]> Signed-off-by: Nikhil G <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: YK <[email protected]>
…on enum (ray-project#58509) PR ray-project#56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config. ex. `yaml.representer.RepresenterError: ('cannot represent an object', <AggregationFunction.MEAN: 'mean'>)` This fix adds 1. a helper function that recursively converts Enum objects to their string values before YAML dump, ensuring proper serialization of all enum types in the configuration. 2. a test that (a) Creates a temporary output YAML file (b) Reads the config from that file (c) Verifies that AggregationFunction.MEAN is correctly serialized as "mean" (string) Fixes ray-project#58485 --------- Signed-off-by: Nikhil Ghosh <[email protected]> Signed-off-by: Nikhil G <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…on enum (ray-project#58509) PR ray-project#56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config. ex. `yaml.representer.RepresenterError: ('cannot represent an object', <AggregationFunction.MEAN: 'mean'>)` This fix adds 1. a helper function that recursively converts Enum objects to their string values before YAML dump, ensuring proper serialization of all enum types in the configuration. 2. a test that (a) Creates a temporary output YAML file (b) Reads the config from that file (c) Verifies that AggregationFunction.MEAN is correctly serialized as "mean" (string) Fixes ray-project#58485 --------- Signed-off-by: Nikhil Ghosh <[email protected]> Signed-off-by: Nikhil G <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: peterxcli <[email protected]>
PR #56871 introduced the AggregationFunction enum for autoscaling metrics aggregation. However, the serve build command's YAML serialization path was not updated to handle Enum objects, causing RepresenterError when serializing AggregationFunction enum values in autoscaling config.
ex.
yaml.representer.RepresenterError: ('cannot represent an object', <AggregationFunction.MEAN: 'mean'>)This fix adds
(a) Creates a temporary output YAML file
(b) Reads the config from that file
(c) Verifies that AggregationFunction.MEAN is correctly serialized as "mean" (string)
Fixes #58485