Skip to content

Say a response's schema after the prompt for HTTP models - #377

Open
akbashev wants to merge 2 commits into
huggingface:mainfrom
akbashev:fix/schema-in-prompt
Open

akbashev wants to merge 2 commits into
huggingface:mainfrom
akbashev:fix/schema-in-prompt

Conversation

@akbashev

Copy link
Copy Markdown
Contributor

Fixes #369. HTTP models (Ollama, OpenAI, Open Responses, Anthropic, Gemini) now honor includeSchemaInPrompt: for a response that isn't String, the schema instructions are added to the last prompt entry of the request's transcript. The schema is still sent to the server as before.

The private schemaPrompt(for:) copies in MLX, llama.cpp and Core ML are now one shared GenerationSchema.schemaPrompt(). It says the inlined schema, or, when the schema can't be inlined, as with a recursive one, the schema as it's encoded, with its definitions.

Tested for every HTTP model, streamed and not, after a tool round and with includeSchemaInPrompt: false, plus a recursive schema's prompt. The plain-text fallback and the retry from the issue's discussion aren't part of this.

includeSchemaInPrompt was honored by the local models and Foundation Models,
but the HTTP models only sent the schema to the server. A server that ignores
it, such as an OpenAI-compatible API or one of Ollama's cloud models, then
gets a model that answers in prose. Ollama, OpenAI (both variants), Open
Responses, Anthropic and Gemini now also say the schema after the prompt being
answered, and still send it to the server. Said there, next to what's asked, a
model follows it even after rounds of tool calls, where some overlook a system
message. Free text has no schema to say.

The three local models' copies of the schema prompt become one,
GenerationSchema.schemaPrompt(), which says the inlined schema.
A recursive schema can't be inlined, so the shared schema prompt said only "Respond with valid JSON only." It now says the schema as it's encoded, with its definitions, as the local models' helpers did.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP models ignore includeSchemaInPrompt, so structured output fails on servers that don't enforce schemas

1 participant