chore(internal): simplify http snapshots#1092
Conversation
73d3f9a to
87759a3
Compare
| "x-robots-tag": "none", | ||
| "server": "cloudflare" | ||
| }, | ||
| "body": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"model\":\"claude-haiku-4-5-20251001\",\"id\":\"msg_01SAGfXDtnWyD3j4h5Wz3kky\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":656,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"output_tokens\":26,\"service_tier\":\"standard\"}} }\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_01LZ7WbFLVzFd16Ur9RHXeGz\",\"name\":\"get_weather\",\"input\":{}} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"loca\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"tion\\\": \\\"Sa\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"n Fran\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"cisco, CA\\\"\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\", \\\"units\\\"\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\": \\\"f\\\"}\"} }\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0 }\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":656,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"output_tokens\":74} }\n\nevent: message_stop\ndata: {\"type\":\"message_stop\" }\n\n" |
There was a problem hiding this comment.
I'll add a serializer for text/event-stream content-type, so it'll be pretty printed too
There was a problem hiding this comment.
You planning on doing that in a follow-on? or in this PR too
There was a problem hiding this comment.
I'll do it in a different one
| response = await async_stream_parse(async_snapshot_client) | ||
|
|
||
| assert response.content[0].text == snapshot("[12345,67890]") | ||
| assert response.content[0].text == snapshot("[12345,67890]") # type: ignore |
There was a problem hiding this comment.
q: why do we need the type ignore now?
There was a problem hiding this comment.
Ah, that's for accessing the text property, since it might be missing. Idk what's better an extra line to check for it, or just type: ignore. pytest would anyway fail with the good message if there would not be .text 🤷♂️
| { | ||
| "response": { | ||
| "status_code": 200, | ||
| "headers": { |
There was a problem hiding this comment.
q: can / should we filter out a bunch of these headers? some of them will frequently update and aren't helpful, e.g. date
There was a problem hiding this comment.
That's a good point. I can add a whitelist/blacklist for headers, or an option to exclude them entirely. I think in the SDKs, we're more interested in the request headers, not the response headers
There was a problem hiding this comment.
Yeah can you update the default header blacklist to include the following?
daterequest-idanthropic-organization-idx-envoy-upstream-service-timecf-ray
| @@ -0,0 +1,93 @@ | |||
| [ | |||
| { | |||
There was a problem hiding this comment.
q: can / should we include the request here as well?
There was a problem hiding this comment.
Hmmm. I initially disabled it by default for more compact snapshots. Since we won't need request snapshots in every test, maybe it makes more sense to enable it per-test when needed
There was a problem hiding this comment.
ooc how do you enable it for a test? I think we should probably include the requests for the tool runner tests?
There was a problem hiding this comment.
I do...
@pytest.mark.parametrize(
"http_snapshot, http_snapshot_serializer_options",
[
(
inline_snapshot.external("uuid:my-test-snapshot.json"),
SnapshotSerializerOptions(
exclude_request_headers=["X-API-Key"],
include_request=True, # Include request details in snapshot
),
),
],
)There was a problem hiding this comment.
@RobertCraigie Okay, I’ve updated it so that all snapshots now include the request, and it doesn’t seem too messy.
| "x-robots-tag": "none", | ||
| "server": "cloudflare" | ||
| }, | ||
| "body": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"model\":\"claude-haiku-4-5-20251001\",\"id\":\"msg_01SAGfXDtnWyD3j4h5Wz3kky\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":656,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation\":{\"ephemeral_5m_input_tokens\":0,\"ephemeral_1h_input_tokens\":0},\"output_tokens\":26,\"service_tier\":\"standard\"}} }\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_01LZ7WbFLVzFd16Ur9RHXeGz\",\"name\":\"get_weather\",\"input\":{}} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"loca\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"tion\\\": \\\"Sa\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"n Fran\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"cisco, CA\\\"\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\", \\\"units\\\"\"} }\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\": \\\"f\\\"}\"} }\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0 }\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":656,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"output_tokens\":74} }\n\nevent: message_stop\ndata: {\"type\":\"message_stop\" }\n\n" |
There was a problem hiding this comment.
You planning on doing that in a follow-on? or in this PR too
| return self.__class__.__name__.split("[", maxsplit=1)[0] | ||
|
|
||
| with monkeypatch.context() as m: | ||
| with pytest.MonkeyPatch.context() as m: |
There was a problem hiding this comment.
q: does this still work as a test-scoped patch? I did the forward monkeypatch setup before to make sure any patches are cleaned up properly; If this works too that would be cool.
There was a problem hiding this comment.
I did the forward monkeypatch setup before to make sure any patches are cleaned up properly
It cleans up immediately after exiting the context manager, in both cases
There was a problem hiding this comment.
oh very nice, I missed the context manager 🤦
5302f27 to
9b5ab24
Compare
1dd6119 to
cd1b39b
Compare
f6ce67c to
4de03c2
Compare
11678b3 to
1eaae19
Compare
|
@claude can I get your review? |
3c684e1 to
6eaf2e3
Compare
|
@claude review this PR please |
6eaf2e3 to
b066374
Compare
RobertCraigie
left a comment
There was a problem hiding this comment.
I only really skimmed but this looks great!
Before:
After: