toolkitbox

~/ / AI articles / Local LLMs on a VPS / Calling a local model from your code: structured outputs and the OpenAI-compatible API in Ollama

Calling a local model from your code: structured outputs and the OpenAI-compatible API in Ollama

Once Ollama runs on your server, your own code needs two things: a way to get answers in a fixed shape, and a way to reach the server from libraries that already speak the OpenAI API. Ollama documents both. This article summarizes the structured outputs page and the OpenAI compatibility page, and points out what the documentation says is not supported.

Published: · 4 min read · Checked against the official Ollama documentation on 2026-10-26. The supported features change between versions, so confirm them in the current documents.

Two ways to call the same server

Ollama has its own API, where the examples in the documentation POST to http://localhost:11434/api/chat. It also offers an OpenAI-compatible API under /v1/, so that existing OpenAI clients can talk to it by changing the base URL.

ItemNative APIOpenAI-compatible API
Base addresshttp://localhost:11434/api/chathttp://localhost:11434/v1/
API keynot mentioned in the examplesapi_key='ollama', which the page marks as required but ignored
Structured output fieldformatresponse_format

The API key line comes from the documentation's local-server example. For the cloud, the page shows base_url https://ollama.com/v1 with an API key from your Ollama account, which is a different case.

Structured outputs: the format field

The format field constrains the response. It accepts the string json for generic JSON, or a JSON schema object. In the documentation's cURL examples the request body has four fields: model, messages, stream set to false, and format. The schema in the examples has type, properties and required.

Value of formatEffect according to the documentation
"json"The model answers with generic JSON
a JSON schema objectThe response is constrained to that schema
  • The documentation says it is ideal to also pass the JSON schema as a string in the prompt, to ground the model's response.
  • It also says to lower the temperature, for example to 0, for more deterministic completions.
  • For Python and JavaScript it suggests defining schemas with Pydantic or Zod, so that they can be reused for validation.
  • Vision models accept the same format parameter. The vision example in the documentation passes an image together with a schema.
  • Ollama's cloud currently does not support structured outputs, according to the page.

Before you rely on a schema, check it. The JSON Schema Validator below tells you whether the schema itself is valid and whether a sample answer matches it.

Check the schema you send

A schema with a mistake can fail late, when the first request reaches the model. Paste the schema and a sample of the JSON you expect, and see whether they agree.

Try it: JSON Schema Validator

Follows JSON Schema Draft 2020-12 for the keywords it supports. It reads $ref only inside the same schema.

Everything runs in your browser. The schema and the data are not uploaded.

The OpenAI-compatible endpoints

EndpointWhat the page listsListed as unsupported
/v1/chat/completionschat, streaming, JSON mode, reproducible outputs, vision, tools, reasoning controllogprobs, tool_choice, logit_bias, user, n; image URLs (use base64)
/v1/completionscompletions, streaming, JSON mode, reproducible outputslogprobs, best_of, echo, logit_bias, user, n
/v1/modelslist and retrieve modelsnot listed
/v1/embeddingsstring or string-array input, encoding format, dimensionstoken arrays, user
/v1/responsesstreaming, function-calling tools, reasoning summariesstateful requests: previous_response_id, conversation, truncation
  • On the chat endpoint the page lists these request fields among the supported ones: response_format, tools, stream, stream_options, seed, stop, temperature, top_p, max_tokens, frequency_penalty and presence_penalty.
  • The page states that structured outputs work through the OpenAI-compatible API via response_format.
  • If your code uses a field from the unsupported column, change the code instead of expecting the server to ignore it. The documentation does not say how each unsupported field is handled, apart from speech, where unsupported values return a 400 error.

Tools and the format of tool definitions

Both the native API and the OpenAI-compatible chat endpoint accept tools. If you describe tools in the OpenAI shape, the Tool Definition Checker on this site checks the names, descriptions and parameter schemas against that format before you send them.

When the answer is still not valid JSON

Constraining the output is meant to give you valid JSON, but your own code may still receive text from another model, an older version, or a request without format. The tool below repairs the common faults such as code fences, trailing commas and wrong quotes, and warns when a repair changes the meaning.

Try it: LLM JSON Repair

Indent

Only fixes with one clear answer are applied. Anything else is reported with its position instead of being guessed.

Everything runs in your browser. The text you paste is not uploaded.

A note on access

The documentation's local example says the API key is required but ignored. Our reading, not a statement from the documents: the key does not protect the server, so keep Ollama on its default address and put authentication in front of it if another machine must reach it.

Sources

Read next