Skip to content

Commit 604eca8

Browse files
committed
fix(serve): serialize JsonSchemaFormat with 'schema' alias
Add serialize_by_alias=True to JsonSchemaFormat.model_config to ensure schema_ field serializes as "schema" not "schema_" for OpenAI compatibility. Add test coverage for serialization behavior. Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com> Assisted-by: IBM Bob
1 parent a612f0a commit 604eca8

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

cli/serve/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class JsonSchemaFormat(BaseModel):
6060
strict: bool | None = None
6161
"""Accepted for OpenAI compatibility; currently ignored by ``m serve``."""
6262

63-
model_config = {"populate_by_name": True}
63+
model_config = {"populate_by_name": True, "serialize_by_alias": True}
6464

6565

6666
class ResponseFormat(BaseModel):

test/cli/test_serve_models.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from pydantic import ValidationError
55

6-
from cli.serve.models import FunctionParameters, StreamOptions
6+
from cli.serve.models import FunctionParameters, JsonSchemaFormat, StreamOptions
77

88

99
class TestStreamOptions:
@@ -127,3 +127,30 @@ def test_empty_dict_accepted(self):
127127
"""Test that an empty dict is accepted (though not a useful schema)."""
128128
params = FunctionParameters(root={})
129129
assert params.root == {}
130+
131+
132+
class TestJsonSchemaFormat:
133+
"""Test JsonSchemaFormat serialization uses 'schema' alias, not 'schema_'."""
134+
135+
def test_serialization_uses_schema_alias(self):
136+
"""Verify schema_ serializes as 'schema' in dict and JSON output."""
137+
schema_def = {"type": "object", "properties": {"foo": {"type": "string"}}}
138+
json_schema = JsonSchemaFormat(name="TestSchema", schema=schema_def)
139+
140+
# Dict serialization
141+
dumped = json_schema.model_dump()
142+
assert "schema" in dumped and "schema_" not in dumped
143+
assert dumped["schema"] == schema_def
144+
145+
# JSON serialization
146+
json_str = json_schema.model_dump_json()
147+
assert '"schema":' in json_str and '"schema_":' not in json_str
148+
149+
# Input accepts both 'schema' (alias) and 'schema_' (field name)
150+
from_alias = JsonSchemaFormat(name="Test1", schema={"type": "string"})
151+
# Use model_validate to test runtime populate_by_name behavior (bypasses type checker)
152+
from_field = JsonSchemaFormat.model_validate(
153+
{"name": "Test2", "schema_": {"type": "number"}}
154+
)
155+
assert from_alias.schema_ == {"type": "string"}
156+
assert from_field.schema_ == {"type": "number"}

0 commit comments

Comments
 (0)