Clean JSON Extraction with Ollama and Python
Описание и применение
Техника Anchor Tag Framing гарантирует получение валидного JSON из ответов Ollama, устраняя ошибки парсинга и упрощая интеграцию в рабочие процессы.
Задача
Small open‑source LLMs (e.g., llama3:8b, mistral:7b, phi3) often wrap JSON output in markdown fences, preambles, or incomplete payloads, causing parsing errors in production workflows.
Результат
The Anchor Tag Framing technique ensures 100% valid JSON extraction from Ollama streams, eliminating parsing errors and simplifying downstream processing.
Рабочий процесс
- 1. Define a system prompt that instructs the model to output raw JSON only within unique
<payload>tags and no markdown or preambles. - 2. Send user query to Ollama with the system prompt and user message.
- 3. Receive raw text response from Ollama.
- 4. Run a Python extractor that first searches for
<payload>...</payload>boundaries using a regex. - 5. If tags are missing, fall back to a regex that captures the first JSON object or array in the text.
- 6. Parse the extracted string with json.loads() to obtain a Python dict.
- 7. Use the structured data in downstream applications or store it.
- 8. Handle extraction errors gracefully with try/except.
- 9. Optionally, log or report failures for model fine‑tuning feedback.
Доказательства
If you feed response['message']['content'] directly into json.loads(), your application raises a json.decoder.JSONDecodeError.
By instructing the model to place its JSON payload strictly inside unique, non-colliding XML-style execution tags, we establish a deterministic structural boundary.
- Дата записи: 2026-09-21T15:15:16.606Z