Skip to content

fix(ai): skip JSON parse for Anthropic streamed tool calls without arguments - #5764

Open
pcbeingused333 wants to merge 1 commit into
PostHog:mainfrom
pcbeingused333:fix/anthropic-empty-tool-input
Open

pcbeingused333 wants to merge 1 commit into
PostHog:mainfrom
pcbeingused333:fix/anthropic-empty-tool-input

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Problem

When an Anthropic streamed response calls a tool that takes no arguments, Claude sends no input_json_delta events, so the accumulated inputString is ''. At content_block_stop the wrapper calls JSON.parse(''), which throws, and logs Error parsing tool input: SyntaxError: Unexpected end of JSON input via console.error on every such call. The captured arguments already stays {}, so the only effect is noisy logs in the user's app.

Changes

  • Skip the parse when no input was streamed; arguments keeps its default {}. Invalid non-empty JSON still logs as before.
  • Regression test in tests/anthropic.test.ts: a streamed no-argument tool call must not call console.error. It fails without the change and passes with it.
  • Changeset for @posthog/ai (patch).

Release info Sub-libraries affected

Libraries affected

  • All of them
  • posthog-js (web)
  • posthog-js-lite (web lite)
  • posthog-node
  • posthog-react-native
  • @posthog/react-native-plugin
  • @posthog/react
  • @posthog/ai
  • @posthog/convex
  • @posthog/next
  • @posthog/nextjs-config
  • @posthog/nuxt
  • @posthog/openfeature-node-provider
  • @posthog/openfeature-web-provider
  • @posthog/rollup-plugin
  • @posthog/webpack-plugin
  • @posthog/types
  • @posthog/browser-common

Checklist

  • All commits are signed (see Commit signing)
  • Tests for new code
  • Accounted for the impact of any changes across different platforms
  • Accounted for backwards compatibility of any changes (no breaking changes!)
  • Took care not to unnecessarily increase the bundle size

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Prepared with Claude Code (Anthropic).
  • Verified with the @posthog/ai vitest suite tests/anthropic.test.ts (no live API calls): 1 failure with the previous code, all pass with the fix. No manual testing against the live API.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 17, 2026 13:33
@turnipdabeets
turnipdabeets requested a review from a team November 17, 2026 15:49
@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-node

1 change worth reviewing: 1 observed improvement.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

Test Measurement Before PR With PR Change
Evaluate a cohort-filtered flag Process CPU time per operation 29.64 µs/op 26.32 µs/op 11.2% less time
View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate one boolean flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a rollout flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ V8 samples do not expose total allocation count; sample count is not object count.​; head:​ V8 samples do not expose total allocation count; sample count is not object count.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation (estimated) 10.09 KiB/op 10.04 KiB/op 0.5% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 19.3 µs/op 18.72 µs/op 3% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 102.64 MiB 102.51 MiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 106.52 MiB 106.82 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling 10.15 MiB 10.15 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 106.2 MiB 106.5 MiB 0.3% more memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 35.44 µs/op 34.05 µs/op 3.9% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 106.75 MiB 107.04 MiB 0.3% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation (estimated) 18.14 KiB/op 18.09 KiB/op 0.3% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 34.3 µs/op 34.19 µs/op 0.3% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 102.77 MiB 102.45 MiB 0.3% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 113.92 MiB 113.88 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling 10.16 MiB 10.16 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 109.87 MiB 109.84 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 52.4 µs/op 51.74 µs/op 1.3% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 116.33 MiB 116.09 MiB 0.2% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation (estimated) 10.45 KiB/op 9.86 KiB/op 5.7% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 21.85 µs/op 20.65 µs/op 5.5% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 102.88 MiB 102.82 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 107.44 MiB 106.93 MiB 0.5% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling 10.23 MiB 10.23 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 107.51 MiB 106.99 MiB 0.5% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 39.47 µs/op 37.07 µs/op 6.1% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 107.61 MiB 107.23 MiB 0.4% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation (estimated) 18.16 KiB/op 18.16 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 30.23 µs/op 30.07 µs/op 0.5% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 103 MiB 103.16 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 109.2 MiB 109.37 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling 10.24 MiB 10.24 MiB <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 107.96 MiB 108.18 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 48.12 µs/op 46.7 µs/op 3% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 109.21 MiB 109.43 MiB 0.2% more memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation (estimated) 8.81 KiB/op 8.47 KiB/op 3.9% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 13.61 µs/op 14.65 µs/op 7.6% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Process CPU time per operation 27.21 µs/op 30.77 µs/op 13.1% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation (estimated) 8.67 KiB/op 8.54 KiB/op 1.5% less memory Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 13.71 µs/op 13.94 µs/op 1.6% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Process CPU time per operation 27.34 µs/op 27.71 µs/op 1.3% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation (estimated) 133.96 KiB/op 131.9 KiB/op 1.5% less memory Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 127.83 µs/op 125.85 µs/op 1.6% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 233.16 µs/op 231.08 µs/op 0.9% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation (estimated) 1.23 MiB/op 1.22 MiB/op 0.4% less memory Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 1.02 ms/op 1.03 ms/op 1% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 1.8 ms/op 1.8 ms/op 0.1% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation (estimated) 12.14 MiB/op 12.12 MiB/op 0.1% less memory Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 12.01 ms/op 11.97 ms/op 0.3% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 19.01 ms/op 19.05 ms/op 0.2% more time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 272.71 MiB 272.82 MiB <0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 272.79 MiB 272.89 MiB <0.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling 9.77 MiB 9.77 MiB <0.1% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 272.08 MiB 272.89 MiB 0.3% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 273.87 MiB 273.96 MiB <0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 227.76 MiB 227.41 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 240.45 MiB 239.95 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling 11.81 MiB 11.81 MiB <0.1% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 236.2 MiB 235.7 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 242.95 MiB 242.36 MiB 0.2% less memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation (estimated) 17.05 KiB/op 17.16 KiB/op 0.6% more memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 45.48 µs/op 45.39 µs/op 0.2% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 48.67 µs/op 48.02 µs/op 1.3% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation (estimated) 14.48 KiB/op 14.2 KiB/op 1.9% less memory Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 25.78 µs/op 21.87 µs/op 15.2% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 29.64 µs/op 26.32 µs/op 11.2% less time Worth reviewing: observed improvement
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation (estimated) 17.52 KiB/op 17.62 KiB/op 0.6% more memory Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 25.34 µs/op 27.22 µs/op 7.4% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 28.62 µs/op 30.04 µs/op 5% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation (estimated) 22.21 KiB/op 22.31 KiB/op 0.5% more memory Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 29.21 µs/op 26.6 µs/op 8.9% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 32.33 µs/op 29.99 µs/op 7.2% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation (estimated) 16.83 KiB/op 16.9 KiB/op 0.5% more memory Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 3.55 µs/op 3.55 µs/op <0.1% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 6.9 µs/op 6.89 µs/op <0.1% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation (estimated) 12.63 KiB/op 12.71 KiB/op 0.7% more memory Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 23.18 µs/op 24.43 µs/op 5.4% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 26.74 µs/op 27.75 µs/op 3.8% more time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation (estimated) 18.19 KiB/op 18.27 KiB/op 0.4% more memory Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 49.5 µs/op 45.18 µs/op 8.7% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 52.38 µs/op 48.17 µs/op 8% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation (estimated) 14.39 KiB/op 14.09 KiB/op 2.1% less memory Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 33.16 µs/op 33.08 µs/op 0.2% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 36.41 µs/op 36.26 µs/op 0.4% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation (estimated) 17.33 KiB/op 17.55 KiB/op 1.2% more memory Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 40.31 µs/op 42.15 µs/op 4.6% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 44.36 µs/op 45.57 µs/op 2.7% more time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared fce957b9a965103031cd90d7b2934cad43f010e2 → 3405d06dcc317930ab594f761c2553cc90389b47.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

This branch has not been deployed

No deployments
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.

1 participant