ollama-structured-output-test
A Node.js application that demonstrates how to get structured JSON responses from Ollama using Zod schemas.
A Node.js application that demonstrates how to get structured JSON responses from Ollama using Zod schemas. It queries a locally or remotely running Ollama model and returns a structured response built from a defined schema, shown with an example that asks about Disney World and returns fields such as name, city, attractions, rides, and fun facts. It relies on the ollama JavaScript client, zod for schema validation, zod-to-json-schema to convert schemas to JSON Schema, and dotenv for environment configuration. The Ollama host and model are set via a .env file.
Setup
- Clone the repository
- Install dependencies:
npm ci
- Update the
.envfile in the root directory with your Ollama host and model:
OLLAMA_HOST=http://localhost:11434 # Or your Ollama host
OLLAMA_MODEL=llama3.2:1b # Or your preferred model