Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Clean JSON Extraction with Ollama and Python

说明与用途

Anchor Tag Framing 技术确保从 Ollama 响应中提取 100% 有效 JSON,消除解析错误并简化后续集成。

问题

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