If a script expects JSON from a local model, a prompt alone can leave you handling code fences, missing keys, or unexpected values. Ollama’s structured-output feature lets you pass a JSON schema using the format field. Your application should still parse and validate the response.


Start with a small schema


For a document triage tool, ask for a short summary and a category chosen from a fixed set. Mark both fields required and use an enum for the category. Explain the task in the prompt and provide the schema through format. A compact schema makes it easier to spot which assumption failed.


Validate structure and meaning separately


If the output fails validation, return a clear error or retry with a limited budget. If the JSON is valid but the category does not reflect the source document, treat that as a quality failure. Test ambiguous and incomplete inputs before allowing the output to drive another tool. Use the local model path covered by the documentation and confirm support before switching providers.


Ollama documentation: https://docs.ollama.com/capabilities/structured-outputs