Skip to content

fix(memory): handle JSON-string tags from LLM serialization (v1.2.1) - #52

Merged
xlabtg merged 3 commits into
xlabtg:mainfrom
konard:issue-51-e97d8346d32d
Mar 26, 2026
Merged

fix(memory): handle JSON-string tags from LLM serialization (v1.2.1)#52
xlabtg merged 3 commits into
xlabtg:mainfrom
konard:issue-51-e97d8346d32d

Conversation

@konard

@konard konard commented Mar 26, 2026

Copy link
Copy Markdown

Summary

Fixes #51

Root Cause

The error tags: must be array occurs because LLMs sometimes serialize array arguments as a JSON-encoded string instead of a proper JSON array:

// What the plugin expects:
{ "tags": ["rules", "github"] }

// What LLMs sometimes send:
{ "tags": "[\"rules\", \"github\"]" }

The external validation layer in @mariozechner/pi-ai checks the JSON Schema before calling execute, so the plugin never had a chance to handle this gracefully. The issue is intermittent because LLMs don't do this consistently — they sometimes pass a real array, sometimes a string.

Fix

Two complementary changes:

  1. Schema widened — Each tags parameter schema is changed from type: "array" to oneOf: [array, string], so the validator accepts both forms and lets execute run.

  2. coerceToArray() helper added — Normalises a JSON string, plain string, or proper array to a plain JS array before processing. Used in parseTags() (for memory_store / memory_update) and in memory_search tag filter normalisation.

Tests Added

  • memory_store — accepts tags as a JSON-encoded string '["rules", "github"]'
  • memory_store — accepts tags as a single plain string "work"
  • memory_search — accepts tags filter as a JSON-encoded string '["github"]'

All 62 tests pass.


This PR was created automatically by the AI issue solver

konard and others added 2 commits March 26, 2026 07:12
Adding .gitkeep for PR creation (default mode).
This file will be removed when the task is complete.

Issue: xlabtg#51
LLMs sometimes serialize array arguments as a JSON string
(e.g. '["rules","github"]' instead of ["work","urgent"]).
The external validation layer rejects these with "tags: must be array"
before the plugin ever runs, causing the intermittent failure.

- Add coerceToArray() helper that parses a JSON-string into an array,
  or treats a plain string as a single-element array
- Use coerceToArray() in parseTags() so memory_store and memory_update
  handle string-typed tags inputs gracefully
- Use coerceToArray() in memory_search tag filter normalization
- Widen the JSON Schema for every `tags` parameter from `type: "array"`
  to `oneOf: [array, string]` so the validator accepts both forms
- Add three new tests covering JSON-string tags in memory_store (two cases)
  and memory_search

Fixes xlabtg#51

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard changed the title [WIP] Bug when the agent is working with the memory plugin - Memory Plugin Validation Error 🧠 fix(memory): handle JSON-string tags from LLM serialization (v1.2.1) Mar 26, 2026
@konard
konard marked this pull request as ready for review March 26, 2026 07:16
@konard

konard commented Mar 26, 2026

Copy link
Copy Markdown
Author

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $0.971547
  • Calculated by Anthropic: $0.662551 USD
  • Difference: $-0.308995 (-31.80%)

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: sonnet
  • Model: Claude Sonnet 4.6 (claude-sonnet-4-6)

📎 Log file uploaded as Gist (943KB)


Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard

konard commented Mar 26, 2026

Copy link
Copy Markdown
Author

✅ Ready to merge

This pull request is now ready to be merged:

  • All CI checks have passed
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@xlabtg
xlabtg merged commit 6c2c33d into xlabtg:main Mar 26, 2026
6 checks passed
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.

Bug when the agent is working with the memory plugin - Memory Plugin Validation Error 🧠

2 participants