Skip to content

Structured output does not follow the specified order of the JSON schema #130

@mexpex

Description

@mexpex

Package: lmstudio-1.4.1
LM Studio: 0.3.20(Build 4)

Issue: A structured output does not follow the specified order of the JSON schema provided. Instead the response structure is in alphabetical order. The schema works fine if I use it directly in LM Studio Client.

The current workaround is to add a prefix to the objects in the schema if I want to keep the order.
Tested with various models.

Simple example case:

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string", "description": "Name of the subject"},
        "age": {"type": "string", "description": "Age"},
        "a_summary": { # a_ to showcasae alphabetical order
            "type": "string",
            "description": "Short summary of the person's greatest achievement",
        },
    },
    "required": ["name", "age", "a_summary"],
}

chat_session = lms.Chat()
chat_session.add_system_prompt("You are a helpful AI assistant")

user_input = chat_session.add_user_message("Tell me about a famous person")

result = model.respond(
    chat_session, config={"temperature": 0.4}, response_format=schema
)

print(json.dumps(result.parsed, indent=2))

In this case "a_summary" ends up first in the response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions