You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gptel: Add JSON response schema parsing and preprocessing
Add experimental support for structured outputs to `gptel-request'
via the :schema argument. It is now possible to force an LLM to
respond in JSON conforming to the provided schema.
Note: This commit only adds the infrastructure required for the
feature! No backend currently respects :schema. Support for all
backends will be added in the next commit.
There are several caveats with this feature in its current form:
1. Not all providers support it, but the major backends do:
OpenAI, Anthropic, Gemini, llama-cpp and Ollama.
Support for structured outputs among other "OpenAI-compatible"
backends is flaky.
2. `gptel-send' does not yet support structured outputs, as it is
intended to be a general chat command. Only the `gptel-request'
API does. (Schema support for `gptel-send' can be added if there
is sufficient demand.)
3. Schemas whose root elements are of type array are not
supported by most APIs. In this case the schema is wrapped in an
object with one field and it is the caller's responsibility to
extract the array elements from it.
4. The JSON schema has to be supplied in one of two ways:
- As an elisp object consisting of nested plists, similar to how
arguments in gptel-tool definitions are provided.
- As a JSON schema serialized to a string.
While expressive, both formats are cumbersome for quick use, so
support for other short hand specifications is planned.
* gptel.el (gptel--with-buffer-copy-internal): Copy
`gptel--schema' as well.
(gptel--schema, gptel-request): Add :schema argument, use the
internal variable `gptel--schema' to communicate this to the
payload builders (primarily `gptel--request-data'). The docstring
for :schema is inadequate, but it will require too many lines in
an already long description. This will be updated after adding
other ways to specify the schema.
(gptel--parse-schema): Generic function to parse a provided schema
into a backend-appropriate format.
(gptel--preprocess-schema, gptel--dispatch-schema-type): Utility
functions to sanitize provided schemas. The former is required to
convert all symbols in the spec to strings (see
`gptel--preprocess-tool-args' for why). The latter handles
schemas provided as serialized JSON, and wraps a root-level array
specification in an object. This wrapping is needed since most
APIs require an object type at the schema root.
(gptel--tool-use-p, gptel--tool-result-p): Don't check for
`:tools' in INFO, as the Anthropic API uses an ersatz tool to
provide JSON output as tool call arguments. This ersatz tool is
not defined by the user and not included in `:tools'.
(gptel--handle-tool-use): Handle JSON output masquerading as a
tool call. This is for the Anthropic API only.
0 commit comments