-
Notifications
You must be signed in to change notification settings - Fork 114
feat: cli OpenAI-compatible API response_format support
#884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c85008e
79f327f
af7e905
bfcd500
e82bdc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,8 +29,26 @@ class ToolFunction(BaseModel): | |
| function: FunctionDefinition | ||
|
|
||
|
|
||
| class JsonSchemaFormat(BaseModel): | ||
| """JSON Schema definition for structured output.""" | ||
|
|
||
| name: str | ||
| """Name of the schema.""" | ||
|
|
||
| schema_: dict[str, Any] = Field(alias="schema") | ||
| """JSON Schema definition.""" | ||
|
|
||
| strict: bool | None = None | ||
|
markstur marked this conversation as resolved.
|
||
| """Accepted for OpenAI compatibility; currently ignored by ``m serve``.""" | ||
|
|
||
| model_config = {"populate_by_name": True} | ||
|
|
||
|
|
||
| class ResponseFormat(BaseModel): | ||
| type: Literal["text", "json_object"] | ||
| type: Literal["text", "json_object", "json_schema"] | ||
|
|
||
| json_schema: JsonSchemaFormat | None = None | ||
| """JSON Schema definition when type is 'json_schema'.""" | ||
|
|
||
|
|
||
| class StreamOptions(BaseModel): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: A from pydantic import model_validator
class ResponseFormat(BaseModel):
type: Literal["text", "json_object", "json_schema"]
json_schema: JsonSchemaFormat | None = None
@model_validator(mode="after")
def _require_json_schema(self) -> "ResponseFormat":
if self.type == "json_schema" and self.json_schema is None:
raise ValueError("json_schema is required when type is 'json_schema'")
return selfThe endpoint 's existing Not blocking — happy to ship as-is if the author prefers endpoint-level validation. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scoped nit on the schema-conversion error handling (the broader
except Exceptionhandler at L252 is pre-existing, not in scope for this PR).L181 catches only
ValueErrorfromjson_schema_to_pydantic, but the converter can also surface non-ValueErrorexceptions for pathological schemas that should still be client errors:create_model(...)can raisePydanticUserError/PydanticSchemaGenerationErroron schemas that produce an internally-inconsistent model.Enum(...)in_enum_annotationcan raiseTypeErrorfor certain member-name edge cases (see the enum-collision comment above).These currently fall through to the generic 500 handler and surface to the client as
"Internal server error: ..."rather than a clean 400"Invalid JSON schema".Minimal fix — widen this block only:
Or import the Pydantic error types explicitly and include them in the tuple. Either way keeps the scope tight to schema-conversion failures and leaves the outer handler alone.