ollama - 💡(How to fix) Fix `bge-m3` returns HTTP 500 with `json: unsupported value: NaN` when embedding certain markdown files. [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15582Fetched 2026-04-16 06:36:00
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
labeled ×1

Error Message

The file is valid UTF-8 markdown (~1.9 KB) with standard YAML frontmatter. Other files in the same corpus embed without issue. The same file consistently triggers the error across restarts — it's deterministic, not a transient failure. The error suggests the model produced a NaN value in the embedding vector that Ollama's JSON serializer cannot encode. This is model-level numerical instability for specific inputs, not a client or file issue. Error: Expected: valid embedding vector, or a descriptive error identifying the problematic input

Code Example

import ollama
client = ollama.Client(host="http://your-ollama-host")
with open("triggering_file.md") as f:
    text = f.read()
result = client.embed(model="bge-m3", input=[text])

---

`failed to encode response: json: unsupported value: NaN (status code: 500)`
RAW_BUFFERClick to expand / collapse

What is the issue?

bge-m3 returns HTTP 500 with json: unsupported value: NaN when embedding certain markdown files.

The file is valid UTF-8 markdown (~1.9 KB) with standard YAML frontmatter. Other files in the same corpus embed without issue. The same file consistently triggers the error across restarts — it's deterministic, not a transient failure.

The error suggests the model produced a NaN value in the embedding vector that Ollama's JSON serializer cannot encode. This is model-level numerical instability for specific inputs, not a client or file issue.

Error: ollama._types.ResponseError: failed to encode response: json: unsupported value: NaN (status code: 500)

Reproducer:

import ollama
client = ollama.Client(host="http://your-ollama-host")
with open("triggering_file.md") as f:
    text = f.read()
result = client.embed(model="bge-m3", input=[text])

Expected: valid embedding vector, or a descriptive error identifying the problematic input Actual: HTTP 500, process/watcher crashes

Relevant log output

`failed to encode response: json: unsupported value: NaN (status code: 500)`

OS

Linux

GPU

Nvidia

CPU

AMD

Ollama version

0.20.2

extent analysis

TL;DR

The issue can be mitigated by preprocessing the input text to handle potential numerical instability in the bge-m3 model.

Guidance

  • Verify that the input text does not contain any special characters or sequences that could cause the model to produce NaN values.
  • Check the YAML frontmatter in the markdown file for any potential issues, such as invalid or missing values.
  • Consider adding input validation or sanitization to the client.embed method to handle edge cases.
  • Test the embedding process with a simplified version of the input text to isolate the cause of the issue.

Example

import ollama
client = ollama.Client(host="http://your-ollama-host")
with open("triggering_file.md") as f:
    text = f.read()
# Simplify the input text to test for edge cases
simplified_text = text.replace("\n", " ").replace("\t", " ")
result = client.embed(model="bge-m3", input=[simplified_text])

Notes

The root cause of the issue is likely related to the bge-m3 model's numerical instability for specific inputs. The provided reproducer and log output suggest that the error occurs during the encoding of the response, indicating a problem with the model's output.

Recommendation

Apply a workaround by preprocessing the input text to handle potential numerical instability in the bge-m3 model, as this is a more feasible solution given the current information.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING