Skip to content

fix(memory): keep embedding bytes out of recall results - #7308

Merged
kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/rc18-recall-bytes
Nov 12, 2026
Merged

kovtcharov-amd merged 2 commits into
amd:mainfrom
kovtcharov:kalin/rc18-recall-bytes

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Asking about a memory that isn't there could end the turn with only
"TypeError: Object of type bytes is not JSON serializable": a recall that
returned many rows handed the model each row's stored embedding, and
shrinking that large result to fit the context crashed. Recall now returns
text only, and a large tool result is measured with the same encoder as a
small one, so a date, path, numpy number or bytes value in a big answer no
longer ends the turn.

The hybrid search drops each vector hit's embedding BLOB before it becomes
a result, and the condenser measures a result with the agent's own encoder
instead of a bare JSON encode.

Closes #7283

Test plan

  • python -m pytest tests/unit/test_recall_result_is_json.py tests/unit/agents/test_chunk_index.py tests/unit/agents/test_large_tool_result_truncation.py (185 pass; the new cases fail on main)
  • Rebuilt app with 300+ memories: ask it to list everything it remembers; the turn finishes with an answer
  • Eval categories to watch on the next RC: memory, gaia_memory, rag_quality

Asking about a memory that isn't there could end the turn with only
"TypeError: Object of type bytes is not JSON serializable": a recall that
returned many rows handed the model each row's stored embedding, and
shrinking that large result to fit the context crashed. Recall now returns
text only, and a tool result that still carries a value JSON can't hold
fails with the tool's name and the field's path instead of a bare TypeError.

The hybrid search drops each vector hit's embedding BLOB before it becomes
a result, and condense_result checks the whole result before measuring it.

Closes #ISSUE
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

The memory fix itself is right and complete: recall no longer returns each row's raw embedding bytes, so a big "what do you remember" answer won't crash the turn. The new safety check that comes with it is the problem. It only runs on results big enough to be shrunk, and it is stricter than the converter the agent already uses on every other result. So a tool that returns a date, a file path or a numpy number now works while its answer is small and ends the turn once the answer gets big. That is the same kind of size-dependent crash this PR fixes.

  • What to do: pick one rule for every result size. Either run the check on all tool results, small ones too, or have the shrinking step use the same converter as everything else. Whichever you choose, add a test with a large list of records that contains a date.

Real-world evidence

N/A: no evidence bundle was produced for this run. This verdict rests on reading the code alone. I couldn't run the new tests here because the runner has no Python environment with gaia/pytest. The PR's own unchecked item (a rebuilt app with 300+ memories, listing everything it remembers) is the end-to-end proof still owed.

🔍 Technical details

🟡 require_json_values turns large record results that used to work into a turn-ending TypeError (src/gaia/agents/base/chunk_index.py:1066)

_handle_large_tool_result measures every result with default=self._json_serialize_fallback, which maps bytes to <binary data: N bytes>, datetimes to isoformat(), numpy scalars and arrays to Python values, and objects to __dict__. Before this PR, a large list, or a dict with no string field, left find_body at its early return None and went to _drop_records_with_index, which serializes with that same fallback, so it condensed fine. Only dicts with a text field reached the bare _cost(result) at chunk_index.py:1028 and crashed.

Now condense_result calls require_json_values before find_body, so every over-threshold result is checked, including lists. Two examples:

  • [{"id": i, "when": datetime(...)} for i in range(200)] used to condense. It now raises.
  • A numpy.int64 (not an int subclass) or a pathlib.Path anywhere in a large result now raises. Under the threshold, the same result still passes through the fallback.

Two consistent options:

  1. Fail loudly everywhere: call require_json_values at the top of _handle_large_tool_result for every dict or list result (and decide what happens to _json_serialize_fallback). This has a wider reach, so check tool and MCP results first.
  2. Keep the existing contract: pass the caller's serialize into find_body and measure with len(serialize(result)) instead of _cost(result) at line 1028, which is the line that actually crashed. Then test_a_tool_result_carrying_bytes_names_the_tool_and_field would need to change, since {"results": rows} takes the record path.

Checked and fine

  • get_items_with_embeddings has one caller (memory.py:2352), so dropping embedding there covers every recall path. BM25 rows from store.search don't carry the BLOB.
  • The new test file sits in tests/unit/, so the unit lane collects it.
  • No docs describe the old behaviour, so there is no doc drift.

Strengths

  • The root cause is fixed at the source, where the rows are built for the model, not by covering it up downstream.
  • The error names the tool and the exact field path (results[0].embedding), which turns a bare "bytes is not JSON serializable" into something a developer can act on.
  • The tests use a real MemoryStore with real BLOBs instead of mocking the store.

The JSON check added with the recall fix ran only on results big enough
to condense, and was stricter than the encoder every other result goes
through. A tool returning a date, a path or a numpy number worked while
its answer was small and ended the turn once it grew. The check is gone;
the condenser now measures a result with the same encoder the agent uses
at every size, which is what the original crash needed.
@kovtcharov

Copy link
Copy Markdown
Contributor Author

A large tool result is now handled by the same rule as a small one, so a date, a path or a numpy number in a big answer no longer ends the turn.

  • Removed the new strict check. The condenser now measures a result with the agent's own encoder, the one every result already goes through, instead of a bare JSON encode. The crash came from that bare encode, so this fixes the original failure without adding a size-dependent rule. Recall still drops the embedding bytes at the source.
  • The two tests that expected a TypeError are replaced with tests that large results condense: a 200-record list with dates, numpy ints and paths, a large text result with a date, and a large text result with bytes nested deep, which shows as <binary data: N bytes> just as it does when small.

Still owed from the test plan: the run on a rebuilt app with 300+ memories that asks for everything it remembers.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 12, 2026
Merged via the queue into amd:main with commit 56ce847 Nov 12, 2026
39 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/rc18-recall-bytes branch November 12, 2026 07:43
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
…md#7308

The fix is on main, so a search over 500 memories now returns something JSON
can carry and the strict marker goes. The embedder reload (amd#7288) is the one
left.
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
Every unit test indexes three files and stores five memories, and rc18
fell over at 300 of each: a folder index reloaded the embedder once per
file, so 300 small files took 15 minutes, and a memory search over a few
hundred rows ended the turn on a JSON error. This adds a scale run that
does both at a real user's size on every release tag, with no model: the
installer journeys' fake model server answers, and the test asserts
counts of what GAIA asked it for.

What it asserts:

| At scale | Assertion | Today |
|---|---|---|
| large folder index | every file indexed, exactly one library row each
| ✅ |
| large folder index | the embedder is loaded at most twice, within a
time budget | ✅ (fixed by amd#7310 while this was in review; it caught
amd#7288, a reload per file, before that) |
| search over 500 memories | the result can be sent as JSON | ✅ (fixed
by amd#7308 while this was in review; the test caught it before that) |
| 500 memories | the memory API reaches every one, page by page | ✅ |

**Size:** 300 files, the size rc18 failed at. With amd#7310 in, they index
in about 25 s against the fake server, well inside the 240 s budget;
before it, 300 ran past 45 minutes on a hosted runner (that slowness was
the bug).

**Not built here, and why:** the 60-turn chat with the context meter
compared to what the model is sent (F59) needs a real flagship turn loop
behind the relay, and the memory page showing only 200 of 500 (F63) and
duplicate rows from a re-add during indexing (F65) are a frontend bug
and a race. Each has its own focused test in its fix PR (amd#7309, amd#7312,
amd#7311); a scale version belongs with the Electron journey proposed in
amd#7343.

Stacked on amd#7331 (it uses that PR's `tests/fixtures/fake_lemonade.py`);
the commits before "test: run GAIA at a real user's size ..." are that
PR.

## Test plan

- [ ] The new **Scale Run** workflow on this PR passes (4 passed)
- [ ] Locally: `GAIA_SCALE_FILES=8 python -m pytest
tests/integration/test_scale_run.py -q` passes
- [ ] Runs on every `v*` tag (each RC and final) and on PRs that touch
the test, its fixture or the workflow

<details>
<summary>🔍 Technical details</summary>

- The folder is indexed once per run through `POST
/api/documents/index-folder` with the real RAG code; the embedder's load
and unload requests are read from the fake server's log.
- The time budget (240 s) sits in the same expected-failure test as the
load count, since the reloads are what make it slow.
</details>

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory: a large recall ends the turn with a raw TypeError

2 participants