Repository navigation
fix(recorder): record uncited non-streamed Responses replies instead of error fixtures - #619
Conversation
commit: |
…nothing
A non-streamed Responses reply with no text, tool calls or citations (for
example a refusal-only message or a web_search_call-only reply) was saved
as {content: ""} with no warning. The streamed path already warns in this
case; warn on the JSON path too. What gets saved is unchanged.
|
Thanks for this, it's a clean fix for #615. I pushed two small commits on top:
Red/green: I added two tests in On your two questions:
🤖 Generated with Claude Code |
|
Hey @jpr5, thank you so much for the review and the merge! Adding the warning when a Responses reply comes back empty was a really nice catch. It's great that it now matches the streamed path, and the two tests through the real Agreed on both points. Dropping the old error fixture output completely makes sense, since that output was the bug. Leaving the unknown 2xx body handling for later sounds good to me. If it would help when you get to it, I'm happy to take a look, no rush at all. |
…urn collapses to nothing
An empty non-streamed Anthropic, Gemini or Ollama reply now records as
`{content: ""}`, but it did so silently, while the streamed path logs
"Stream collapse produced empty content" and the Responses JSON path
(CopilotKit#619) logs its own warning.
Log "<Provider> JSON collapse produced empty content — fixture may be
incomplete" on those three empty returns, the same way CopilotKit#619 does. A
cited Anthropic empty-text turn is complete, so it does not warn,
matching the streamed rule. What gets saved does not change.
…error fixtures (#624) Recording a Gemini or Cohere embedding call saved an error, so replays returned that error instead of the vector; now the vector itself is recorded and replayed. Hi! Thank you so much for the quick and kind review on #619, it was a real pleasure. While reading `buildFixtureResponse` for that one I noticed the embedding replies from Gemini and Cohere take the same fallback path, so here is a small follow up in the same spirit. **What this code is for** In `--record` mode, `proxyAndRecord` forwards an unmatched request upstream, relays the reply, and saves it as a fixture. For a JSON reply that was not streamed, it hands the parsed body to `buildFixtureResponse`, which detects the provider shape. For embeddings it only knows OpenAI `data[].embedding` ([L1775](https://github.com/CopilotKit/aimock/blob/d37a31e65da9701144c49ba57817e118660da3ae/src/recorder.ts#L1775-L1800)) and a bare `embedding: [...]` ([L1973-L1976](https://github.com/CopilotKit/aimock/blob/d37a31e65da9701144c49ba57817e118660da3ae/src/recorder.ts#L1973-L1976)).  `buildFixtureResponse` with its one caller `proxyAndRecord`, Functions view in [Architect](https://github.com/abhiramshaji-cyber/Architect) **Before this PR** A test runs `aimock --record` and calls Gemini `embedContent` for "hello". 1. The client sends `POST /v1beta/models/text-embedding-004:embedContent`, no fixture matches, so `proxyAndRecord` forwards it. 2. Upstream answers 200 with `{"embedding":{"values":[0.1,0.2]}}`. 3. `buildFixtureResponse` checks `Array.isArray(obj.embedding)`. Here `embedding` is an object, so it does not match. 4. No later branch matches, so the fallback ([L2525-L2537](https://github.com/CopilotKit/aimock/blob/d37a31e65da9701144c49ba57817e118660da3ae/src/recorder.ts#L2525-L2537)) logs `Could not detect response format from upstream` and saves `{ error: { type: "proxy_error" }, status: 200 }`. 5. The next replay of "hello" serves that error instead of the vector. Cohere `POST /v2/embed` does the same: its reply is `{"embeddings":{"float":[[0.1,0.2]]},"response_type":"embeddings_by_type"}`, which no branch reads. The A7 golden snapshot pins the Gemini case today in `gemini-embedding-record-off` and `gemini-embedding-record-strict-header-off`. **After this PR** Same test, same request. 1. Same as above through step 2. 2. `buildFixtureResponse` sees that `obj.embedding.values` is an array and returns `{ embedding: values }`. 3. The fixture holds the vector. The replay handler in `gemini-embeddings.ts` wraps it back as `{ embedding: { values } }`, so the replayed body is byte for byte what upstream sent. 4. For Cohere, the new branch takes the first vector from `obj.embeddings`, preferring `float` and otherwise the first type whose first entry is an array (`int8`, `uint8`, `binary`, `ubinary`). The replay handler in `cohere.ts` puts it back under every requested type. 5. OpenAI and bare `embedding` replies still hit their branches first and come out unchanged. ```mermaid flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: embedding saved as proxy_error"] direction TB B1["POST embedContent or /v2/embed"] --> B2["proxyAndRecord"] B2 --> B3["buildFixtureResponse"] B3 --> B4{"data array or embedding array?"} B4 -->|"embedding.values or embeddings.float"| B5["proxy_error fixture, replay fails"] B4 -->|"OpenAI data"| B6["embedding fixture"] end subgraph AFTER["AFTER: the vector is recorded"] direction TB A1["POST embedContent or /v2/embed"] --> A2["proxyAndRecord"] A2 --> A3["buildFixtureResponse"] A3 --> A4{"THE CHANGE: embedding.values or embeddings by type?"} A4 -->|"Gemini or Cohere"| A5["embedding fixture, replay serves the vector"] A4 -->|"OpenAI data"| A6["same embedding fixture as before"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827 ``` **The change** Two small branches in `buildFixtureResponse`, right after the bare `embedding` check: one for `obj.embedding.values` (Gemini) and one for the `obj.embeddings` object keyed by type (Cohere v2). Both return the existing `{ embedding }` fixture shape, so neither replay handler changes. The A7 golden is regenerated with `pnpm golden:capture src/__tests__/response-a7-equivalence.test.ts`. Only two rows change: `gemini-embedding-record-off` and `gemini-embedding-record-strict-header-off` now save `{ "embedding": [0.1, 0.2] }` instead of the `proxy_error` fixture. `gemini-embedding-record-strict` is unchanged because strict mode answers 503 before anything is recorded. `src/__tests__/recorder-embeddings.test.ts` records through a stub upstream and replays from a fresh server for each shape: Gemini with values and with an empty array, Cohere `float`, `int8`, `int8` plus `float`, an empty vector, a two text batch, and a `base64` only reply. Seven of the eight fail on `main`. **What changes for a user** - Recorded Gemini `embedContent` and Cohere `/v2/embed` calls now replay the real vector instead of an error. - OpenAI embeddings, Ollama `/api/embed` (which keeps its own json passthrough) and every other reply shape are untouched. - An empty vector is recorded as an empty vector, matching the existing OpenAI and bare `embedding` branches. - A Cohere reply that only has `base64` strings still falls back to the error fixture, since the replay handler would serve numbers under the `base64` key and could not round trip it. - A batched Cohere request keeps only the first vector, and replay repeats it for every text. OpenAI `data[0]` behaves the same way today, so I kept it consistent. Would you prefer multi vector support for batches? I'd be glad to add it here or in a follow up, whichever you like. Thanks again for all your work on aimock, and any feedback or tips are very welcome!
**Merging this publishes @copilotkit/aimock 1.48.1 to npm and aimock-pytest 0.7.3 to PyPI.** ## [1.48.1] - 2026-11-20 ### Changed - `aimock-pytest` 0.7.3 defaults to `@copilotkit/aimock` 1.48.1 ### Fixed - Record uncited non-streamed Responses replies as fixtures, not `proxy_error` (#619) - Record Gemini `embedContent` and Cohere v2 `embed` replies as embeddings, not `proxy_error` (#624) - Record empty non-streamed Anthropic, Gemini and Ollama replies as empty, not `proxy_error` (#623) All three are bug fixes, so this is a patch release. ## Version carriers (from `git grep -F 1.48.0`) Bumped to 1.48.1: - `package.json` version - `charts/aimock/Chart.yaml` appVersion - `.claude-plugin/plugin.json` version - `.claude-plugin/marketplace.json` source version (`^1.48.1`) - `docs/index.html` banner - `packages/aimock-pytest/README.md` `--aimock-version` default - `packages/aimock-pytest/src/aimock_pytest/_version.py` `AIMOCK_VERSION` Also bumped: `packages/aimock-pytest/pyproject.toml` 0.7.2 → 0.7.3, because the pin moved (same pattern as #612). Left as is (not carriers): the `## [1.48.0]` CHANGELOG section. ## CHANGELOG edits - `## [Unreleased]` becomes `## [1.48.1] - 2026-11-20`. - The Responses entry now references #619, the PR that fixed issue #615. The other entries already reference PRs. - Two bullets were shortened to fit the 100-character Slack limit. - An aimock-pytest `Changed` line was added for the pin bump, matching 1.48.0. ## README No new features, so no README change. The `package.json` description already matches the README subtitle. ## Local checks - `pnpm install --frozen-lockfile`, `format:check`, `lint`, `typecheck`, `build`: pass - `vitest run`: 491 files, 20,132 passed, 11 skipped - `pytest` in `packages/aimock-pytest` against the local build: 251 passed
## [1.48.1] - 2026-11-20 ### Changed - `aimock-pytest` 0.7.3 defaults to `@copilotkit/aimock` 1.48.1 ### Fixed - Record uncited non-streamed Responses replies as fixtures, not `proxy_error` (CopilotKit#619) - Record Gemini `embedContent` and Cohere v2 `embed` replies as embeddings, not `proxy_error` (CopilotKit#624) - Record empty non-streamed Anthropic, Gemini and Ollama replies as empty, not `proxy_error` (CopilotKit#623)
Recording a successful Responses API reply that was not streamed saved an error, so replays failed; now the reply itself is recorded and replays like the streamed one.
Thanks so much for #615 and the very clear repro, the
Runner.rundetail and the logged top level keys made this easy to follow.What this code is for
In
--recordmode,proxyAndRecordforwards a request upstream, relays the reply, and saves it as a fixture. For a JSON reply that was not streamed, it hands the parsed body tobuildFixtureResponse, which detects the provider shape and turns it into a fixture response. With--proxy-only, the same code runs butpersistFixturereturnsskippedbefore writing anything.Before this PR
A test runs the OpenAI Agents SDK
Runner.runagainstaimock --record.POST /v1/responseswithstream: false, and upstream answers 200 withobject: "response"and anoutputarray.buildFixtureResponsereaches theArray.isArray(obj.output)branch (L2055-L2088).Could not detect response format from upstream (status=200) — saving as error fixtureand returns{ error: { type: "proxy_error" }, status: 200 }.proxy_errorfor a request that worked.With
--proxy-only, step 4 logs the same "saving" warning, thenpersistFixturereturnsskippedat L358-L361, so nothing was saved.After this PR
Same test, same request.
outputbody withcollapseResponsesJSON, cited or not, and shapes it exactly as the cited path already did:content,toolCalls,customToolCalls, orderedblocksorresponsesBlocks,reasoning,webSearches,usage.citationNotewarning is still logged, now throughlogCollapseWarningslike the streamed path.flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: uncited 200 saved as proxy_error"] direction TB B1["POST /v1/responses, stream false"] --> B2["proxyAndRecord"] B2 --> B3["buildFixtureResponse"] B3 --> B4{"output array with citations?"} B4 -->|"no citations"| B5["proxy_error fixture, replay fails"] B4 -->|"cited"| B6["collapseResponsesJSON fixture"] end subgraph AFTER["AFTER: every Responses reply recorded"] direction TB A1["POST /v1/responses, stream false"] --> A2["proxyAndRecord"] A2 --> A3["buildFixtureResponse"] A3 --> A4{"THE CHANGE: output array?"} A4 -->|"no citations"| A5["content and toolCalls fixture, replay works"] A4 -->|"cited"| A6["same fixture as before plus blocks"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827buildFixtureResponse, its one callerproxyAndRecordand the collapse helpers it now always uses, Functions view in ArchitectThe change
The fix is in
buildFixtureResponse: theif (!collapsed.blocks?.some(hasNativeCitations))split is gone, so the existing cited shaping runs for everyoutputbody. The second change is wording only. The fallback warning, the invalid JSON warning and the base64 embedding warning no longer say "saving as error fixture", since under--proxy-onlynothing is saved and in record modepersistFixturealready logs where the fixture went.I noticed the old comment said the uncited fallback was kept on purpose, "as before citations", so I updated the two
citation-replay.test.tsassertions that pinned it, and addedrecorder-responses-json.test.ts: the reply from the issue (withobject,billing,usageand so on) records and replays, and text, a tool call, text then a tool call, reasoning then text, and an emptyoutputeach record the same fixture as the stream of the same turn.A couple of questions, since you know the history much better than I do:
unknown format falls back to error responsepin it. Would you like a follow up that skips it or saves it as rawjson, or is it better as is?What changes for a user
--recordnow saves uncited Responses replies that were not streamed as normal fixtures, and replays return the reply instead ofproxy_error.--proxy-onlylogs no longer claim a fixture is being saved.format:check,lintandtypecheckpass.pnpm testhas the same 48 failures asmainon my machine (CLI andunreleased-check.ymltests that need a build and full git history), and nothing new fails. Any feedback or tips are very welcome, happy to change anything.Fixes #615