Skip to content

fix(memory): keep numpy scalars out of metadata and API JSON - #1846

Merged
3clyp50 merged 2 commits into
agent0ai:readyfrom
Jehu:fix/memory-float32-json-serialization
Aug 23, 2026
Merged

fix(memory): keep numpy scalars out of metadata and API JSON#1846
3clyp50 merged 2 commits into
agent0ai:readyfrom
Jehu:fix/memory-float32-json-serialization

Conversation

@Jehu

@Jehu Jehu commented Aug 21, 2026

Copy link
Copy Markdown

Memory API handlers crashed with TypeError: Object of type float32 is not JSON serializable whenever they returned document metadata containing similarity scores (e.g. the Memory dashboard search returned a 500 instead of results).

Root cause: memory consolidation stores FAISS relevance scores in doc.metadata['_consolidation_similarity']. The scores are numpy scalars; they were pickled into the FAISS docstore and later failed json.dumps() in ApiHandler.handle_request.

  • The cosine relevance-score normalizer and both metadata assignment sites now coerce scores to native float.
  • handle_request serializes responses with json.dumps(..., default=float), so a numpy scalar left anywhere in a handler payload can no longer produce a 500.

Existing docstores keep their pickled numpy values until each document is rewritten; the default=float fallback keeps them JSON-serializable in the meantime. (Trade-off: a numpy integer in a payload serializes as a float, e.g. 42.0; never observed in practice — only similarity floats carry numpy types.)

Verification

  • Reproduced on a live instance: dashboard search failed with the TypeError above; 41/52 documents in usr/memory/default/index.pkl carried numpy _consolidation_similarity values.
  • One-off conversion of that docstore: all 52 documents round-trip through json.dumps.
  • Verified default=float serializes np.float32, np.float64, and np.int64.
  • python -m py_compile on the three changed files. No automated tests exist for these helpers, so validation was manual.

Memory consolidation stores FAISS relevance scores (numpy scalars) in
document metadata. The values get pickled into the docstore and crash
json.dumps with 'Object of type float32 is not JSON serializable' in
helpers/api.py for handlers that return full metadata. Coerce scores to
native floats at the source and serialize responses with default=float.
@Jehu
Jehu force-pushed the fix/memory-float32-json-serialization branch from 4a93fbc to c26ed80 Compare August 21, 2026 06:57
Keep shared API serialization strict while converting legacy NumPy consolidation scores in the memory dashboard response.

Add regressions for native relevance scores and legacy dashboard metadata without mutating stored documents.
@3clyp50

3clyp50 commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Thanks, the diagnosis and the source casts look right. I pushed follow-up commit 454d45c4 to narrow the compatibility fix.

I removed default=float from the shared API handler because it changed unrelated response types. For example, np.bool_(True) became 1.0, np.int64(42) became 42.0, and large integers could lose precision.

Legacy _consolidation_similarity values are now converted in the memory dashboard formatter instead. The formatter works on a copy, so stored document metadata is not rewritten as a side effect.

I added regression coverage for the cosine normalizer returning a native float, dashboard JSON serialization with a legacy np.float32 score, and preservation of the original stored NumPy value.

Verification:

  • 23 focused memory, dashboard, timezone, and API tests passed
  • 94 tests passed with the PR layered on the current ready branch
  • the PR still merges cleanly

@3clyp50
3clyp50 changed the base branch from main to ready August 23, 2026 15:00
@3clyp50
3clyp50 merged commit fe70025 into agent0ai:ready Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants