Skip to content

When CTX_KNOWLEDGE_ENABLED=true fails to process documents - ZodError #26

@gnomonprime

Description

@gnomonprime

Knowledge Plugin Error: AI_InvalidPromptError when CTX_KNOWLEDGE_ENABLED=true

Bug Description

When enabling the knowledge plugin with CTX_KNOWLEDGE_ENABLED=true, the system fails to process documents during startup with the following error:

AI_InvalidPromptError: Invalid prompt: message must be a CoreMessage or a UI message
Validation error: Type validation failed: Value: [{"role":"system","content":[{"type":"text","text":"..."}]}]

Error Details

Stack Trace:

Error message: [
  {
    "code": "invalid_union",
    "unionErrors": [
      {
        "issues": [
          {
            "code": "invalid_type",
            "expected": "string",
            "received": "array",
            "path": [0, "content"],
            "message": "Expected string, received array"
          }
        ]
      },
      {
        "issues": [
          {
            "received": "system",
            "code": "invalid_literal",
            "expected": "user",
            "path": [0, "role"],
            "message": "Invalid literal value, expected \"user\""
          }
        ]
      }
    ]
  }
]
    at standardizePrompt (file:///home/gnomon/elizaos/node_modules/ai/dist/index.mjs:2139:13)
    at generateText (file:///home/gnomon/elizaos/node_modules/ai/dist/index.mjs:4164:25)
    at generateClaudeWithCaching (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:890:24)
    at generateOpenRouterText (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:811:20)
    at generateText (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:708:22)
    at generateTextOperation (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:1340:28)
    at withRateLimitRetry (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:1476:18)
    at processAndSaveFragments (file:///home/gnomon/elizaos/node_modules/@elizaos/plugin-knowledge/dist/index.js:1163:34)

Environment

  • ElizaOS Version: v1.0.11
  • Knowledge Plugin Version: @elizaos/plugin-knowledge@1.0.7
  • Node.js Version: v23.3.0
  • OS: WSL2 Ubuntu (linux 6.6.87.2-microsoft-standard-WSL2)
  • AI SDK Version: 4.3.16

Configuration

Character Configuration (test.json):

{
  "name": "Eliza",
  "plugins": [
    "@elizaos/plugin-sql",
    "@elizaos/plugin-openrouter",
    "@elizaos/plugin-openai",
    "@elizaos/plugin-twitter",
    "@elizaos/plugin-knowledge",
    "@elizaos/plugin-bootstrap"
  ],
  "knowledge": [
    {
      "path": "docs/README.md",
      "shared": false
    }
  ]
}

Environment Variable:

# Knowledge Plugin Configuration
LOAD_DOCS_ON_STARTUP=true
# Advanced Configuration
CTX_KNOWLEDGE_ENABLED=true
EMBEDDING_PROVIDER=openai
TEXT_EMBEDDING_MODEL=text-embedding-3-small
EMBEDDING_DIMENSION=1536
TEXT_PROVIDER=openrouter
TEXT_MODEL=anthropic/claude-sonnet-4
MAX_CONCURRENT_REQUESTS=30         # Parallel processing limit
REQUESTS_PER_MINUTE=60             # Rate limiting
TOKENS_PER_MINUTE=150000           # Token rate limiting
MAX_INPUT_TOKENS=4000              # Chunk size limit
MAX_OUTPUT_TOKENS=4096             # Response size limit

Expected Behavior

The knowledge plugin should successfully:

  1. Process documents from the knowledge array in the character configuration
  2. Generate contextualized chunks without errors
  3. Create document fragments in the knowledge base

Actual Behavior

  • Knowledge service fails to process any document chunks
  • All chunks (2/2 in this case) fail processing
  • Document processing completes with 0 fragments created
  • System logs show: Failed to process 2 chunks out of 2 for document

Analysis

The error appears to be related to:

  1. Invalid Message Format: The knowledge plugin is constructing prompts with:

    • role: "system" (should be "user", "assistant", or "tool")
    • content as an array instead of a string
  2. AI SDK Compatibility: The prompt format doesn't match the expected CoreMessage or UIMessage schema

  3. Version Mismatch: Potential incompatibility between @elizaos/plugin-knowledge@1.0.7 and the current AI SDK version

Proposed Solutions

  1. Update Knowledge Plugin: Check if a newer version of @elizaos/plugin-knowledge is available that's compatible with the current AI SDK
  2. Fix Prompt Construction: Update the knowledge plugin to construct prompts in the correct format
  3. Add Validation: Implement proper validation of prompt structure before passing to AI SDK

Workaround

Currently, setting CTX_KNOWLEDGE_ENABLED=false allows the system to start normally, but without knowledge processing capabilities.

Additional Context

This error occurs during the document contextualization phase where the knowledge service attempts to generate enriched chunks using AI models. The error suggests that the prompt being passed to the generateText function doesn't conform to the expected message schema.

The specific validation errors indicate:

  • Content should be a string, not an array
  • Role should be "user", "assistant", or "tool", not "system"
  • Missing required fields for tool messages

Reproduction Steps

  1. Create a character configuration with @elizaos/plugin-knowledge in plugins array
  2. Add knowledge documents to the character configuration
  3. Set CTX_KNOWLEDGE_ENABLED=true in environment
  4. Start the ElizaOS agent (LOG_LEVEL=debug elizaos start --character test.json )
  5. Observe the error during document processing

Related Issues

This appears to be related to the AI SDK prompt validation changes mentioned in:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions