Skip to content

fix(recorder): record uncited non-streamed Responses replies instead of error fixtures - #619

Merged
jpr5 merged 4 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/recorder-uncited-responses
Nov 16, 2026
Merged

jpr5 merged 4 commits into
CopilotKit:mainfrom
abhiramshaji-cyber:fix/recorder-uncited-responses

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor

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.run detail and the logged top level keys made this easy to follow.

What this code is for

In --record mode, proxyAndRecord forwards 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 to buildFixtureResponse, which detects the provider shape and turns it into a fixture response. With --proxy-only, the same code runs but persistFixture returns skipped before writing anything.

Before this PR

A test runs the OpenAI Agents SDK Runner.run against aimock --record.

  1. The SDK sends POST /v1/responses with stream: false, and upstream answers 200 with object: "response" and an output array.
  2. buildFixtureResponse reaches the Array.isArray(obj.output) branch (L2055-L2088).
  3. That branch only returns a fixture when a block carries native citations. This reply has none, so it falls through.
  4. No later branch matches, so the last fallback (L2531-L2546) logs Could not detect response format from upstream (status=200) — saving as error fixture and returns { error: { type: "proxy_error" }, status: 200 }.
  5. The fixture is written, and the next replay of that turn returns proxy_error for a request that worked.

With --proxy-only, step 4 logs the same "saving" warning, then persistFixture returns skipped at L358-L361, so nothing was saved.

After this PR

Same test, same request.

  1. Same as above through step 2.
  2. The branch collapses every output body with collapseResponsesJSON, cited or not, and shapes it exactly as the cited path already did: content, toolCalls, customToolCalls, ordered blocks or responsesBlocks, reasoning, webSearches, usage.
  3. The fixture holds the reply text, so the replay returns the model's reply.
  4. A cited reply takes the same code and comes out unchanged. The citationNote warning is still logged, now through logCollapseWarnings like 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:#111827
Loading

buildFixtureResponse with its caller and callees
buildFixtureResponse, its one caller proxyAndRecord and the collapse helpers it now always uses, Functions view in Architect

The change

The fix is in buildFixtureResponse: the if (!collapsed.blocks?.some(hasNativeCitations)) split is gone, so the existing cited shaping runs for every output body. 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-only nothing is saved and in record mode persistFixture already 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.ts assertions that pinned it, and added recorder-responses-json.test.ts: the reply from the issue (with object, billing, usage and so on) records and replays, and text, a tool call, text then a tool call, reasoning then text, and an empty output each 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:

  • Would you rather keep the old uncited fallback in some form, for example behind a flag, for anyone depending on the earlier recordings?
  • The issue also mentions an unknown 2xx body. I left that as an error fixture here, because the frozen A7 and A9 equivalence matrices and unknown format falls back to error response pin it. Would you like a follow up that skips it or saves it as raw json, or is it better as is?

What changes for a user

  • --record now saves uncited Responses replies that were not streamed as normal fixtures, and replays return the reply instead of proxy_error.
  • Cited Responses replies, streamed recordings and every other provider record exactly as before.
  • Upstream error bodies and unknown shapes still become error fixtures, as before.
  • --proxy-only logs no longer claim a fixture is being saved.

format:check, lint and typecheck pass. pnpm test has the same 48 failures as main on my machine (CLI and unreleased-check.yml tests 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

@pkg-pr-new

pkg-pr-new Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@619

commit: 60dfc76

jpr5 added 2 commits November 16, 2026 12:34
…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.
@jpr5

jpr5 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, it's a clean fix for #615.

I pushed two small commits on top:

  • A CHANGELOG line under a new [Unreleased] section.
  • A warning on the non-streamed Responses path when a reply collapses to nothing (no text, no tool calls, no citations), for example a refusal-only message or a web_search_call-only reply. The streamed path already warned in that case. What gets saved doesn't change.

Red/green: I added two tests in recorder-responses-json.test.ts that record those replies through the real proxyAndRecord. On your head (76445ed) both failed because no warning was logged. With the change, both pass. recorder-responses-json and citation-replay pass in full (126 tests), and typecheck, eslint and prettier are clean.

On your two questions:

  • We don't want the old error-fixture output kept behind a flag. That output was the bug.
  • The unknown-2xx-body handling can stay as it is in this PR. We'll track it separately.

🤖 Generated with Claude Code

@jpr5
jpr5 merged commit d37a31e into CopilotKit:main Nov 16, 2026
30 checks passed
@abhiramshaji-cyber

Copy link
Copy Markdown
Contributor Author

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 proxyAndRecord make it easy to trust. Thanks for adding the CHANGELOG line too.

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.

jpr5 added a commit to abhiramshaji-cyber/aimock that referenced this pull request Nov 20, 2026
…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.
jpr5 added a commit that referenced this pull request Nov 20, 2026
…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 and its only caller
proxyAndRecord](https://raw.githubusercontent.com/abhiramshaji-cyber/Architect/pr-assets/oss/2026-11-17/aimock-embeddings-buildfixtureresponse.png)
`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!
@jpr5 jpr5 mentioned this pull request Nov 20, 2026
jpr5 added a commit that referenced this pull request Nov 20, 2026
**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
jpr5 added a commit to AnshulBhati01/aimock that referenced this pull request Nov 23, 2026
## [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)
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.

Recorder saves successful non-streamed Responses API replies as error fixtures

2 participants