Turn on structured output for a model configuration. Supports OpenAI-compatible providers (OpenAI, Groq, Together, x.ai, DeepSeek, Xiaomi, Alibaba (Qwen), Zhipu, Moonshot), Anthropic, and Gemini.
Usage
enable_structured_output(
config,
schema = NULL,
name = "llmr_schema",
method = c("auto", "json_mode", "tool_call"),
strict = TRUE
)Arguments
- config
An llm_config object.
- schema
A named list representing a JSON Schema. If
NULL, OpenAI-compatible providers enforce a JSON object; Gemini switches to JSON mime type; Anthropic only injects a tool when a schema is supplied.- name
Character. Schema/tool name for providers requiring one. Default "llmr_schema".
- method
One of c("auto","json_mode","tool_call"). "auto" chooses the best per provider. You rarely need to change this.
- strict
Logical. Request strict validation when supported (OpenAI-compatible). Strict mode has formal requirements of its own: every object must set
additionalProperties: falseand list all its properties as required. LLMR fills those in automatically where your schema leaves them unspecified (your explicit settings are never overridden); passstrict = FALSEto send the schema verbatim.
Server-side enforcement by provider
OpenAI, Groq, Together, x.ai, and Ollama accept a strict json_schema
response format. DeepSeek, Alibaba (Qwen), Zhipu, Moonshot, and Xiaomi
accept only JSON-object mode; for them the supplied schema drives local
parsing and validation, so the prompt itself should describe the desired
fields. Anthropic enforcement runs through a forced tool call; Gemini
through responseJsonSchema.
Gemini
A supplied schema is sent as responseJsonSchema (standard JSON Schema,
supported by Gemini 2.5+ models) together with the JSON mime type. For an
older model that rejects it, set gemini_enable_response_schema = FALSE in
the config to fall back to JSON-mime-type-only mode (the reply is still
parsed and can be validated locally).
When to use tags instead
For tasks where strict JSON schema is unnecessary or unsupported, consider
llm_mutate() with .tags or llm_mutate_tags() for soft structured output.