lmstudio-ai / lmstudio-bug-tracker

Bug tracking for the LM Studio desktop application
10 stars 3 forks source link

Support for Nested JSON Schema Definitions in API #203

Open zhzLuke96 opened 1 week ago

zhzLuke96 commented 1 week ago

Feature Request: 🙏Thx for the great work! I’ve been using the API in LM Studio a lot, especially with JSON schema—it’s been super useful.

But I noticed the latest version (0.3.5 build4) doesn’t support nested JSON schema definitions, eg looped structures like a "UI generate" component. Adding this support would really boost flexibility and functionality in the API.

Here’s a reproduction method and the logs I encountered.

Reproduction:

This is the official demo example from the openai api documentation https://platform.openai.com/docs/guides/structured-outputs?context=ex3#examples

curl http://localhost:1234/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-2024-08-06",
"messages": [
{
"role": "system",
"content": "You are a UI generator AI. Convert the user input into a UI."
},
{
"role": "user",
"content": "Make a User Profile Form"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "ui",
"description": "Dynamically generated UI",
"schema": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "The type of the UI component",
"enum": ["div", "button", "header", "section", "field", "form"]
},
"label": {
"type": "string",
"description": "The label of the UI component, used for buttons or form fields"
},
"children": {
"type": "array",
"description": "Nested UI components",
"items": {"$ref": "#"}
},
"attributes": {
"type": "array",
"description": "Arbitrary attributes for the UI component, suitable for any element",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the attribute, for example onClick or className"
},
"value": {
"type": "string",
"description": "The value of the attribute"
}
},
"required": ["name", "value"],
"additionalProperties": false
}
}
},
"required": ["type", "label", "children", "attributes"],
"additionalProperties": false
},
"strict": true
}
}
}'

Error Log:

2024-11-14 15:20:10 [DEBUG]
1 Error predicting: g [Error]: Invalid JSON Schema: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    |     property 'children' -> object with constructor 'Object'
    --- property 'items' closes the circle
    at y (C:\Users\Administrator\AppData\Local\LM-Studio\app-0.3.5\resources\app\.webpack\main\llmworker.js:8:313256)
    at async t.temp_createLlamaPredictionArgs (C:\Users\Administrator\AppData\Local\LM-Studio\app-0.3.5\resources\app\.webpack\main\llmworker.js:8:316782)
    at async t.LLMEngineWrapper.predictTokens (C:\Users\Administrator\AppData\Local\LM-Studio\app-0.3.5\resources\app\.webpack\main\llmworker.js:10:24709)
    at async Object.predictTokens (C:\Users\Administrator\AppData\Local\LM-Studio\app-0.3.5\resources\app\.webpack\main\llmworker.js:10:50931)
    at async Object.handleMessage (C:\Users\Administrator\AppData\Local\LM-Studio\app-0.3.5\resources\app\.webpack\main\llmworker.js:10:44879) {
  cause: undefined,
  suggestion: undefined,
  errorData: undefined,
  data: undefined,
  displayData: undefined,
  title: 'Invalid JSON Schema: Converting circular structure to JSON\n' +
    "    --> starting at object with constructor 'Object'\n" +
    "    |     property 'properties' -> object with constructor 'Object'\n" +
    "    |     property 'children' -> object with constructor 'Object'\n" +
    "    --- property 'items' closes the circle"
}
2024-11-14 15:20:10 [ERROR]
Error: Invalid JSON Schema: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    |     property 'children' -> object with constructor 'Object'
    --- property 'items' closes the circle