-
Notifications
You must be signed in to change notification settings - Fork 10
Description
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:
- Process documents from the
knowledgearray in the character configuration - Generate contextualized chunks without errors
- 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:
-
Invalid Message Format: The knowledge plugin is constructing prompts with:
role: "system"(should be "user", "assistant", or "tool")contentas an array instead of a string
-
AI SDK Compatibility: The prompt format doesn't match the expected
CoreMessageorUIMessageschema -
Version Mismatch: Potential incompatibility between
@elizaos/plugin-knowledge@1.0.7and the current AI SDK version
Proposed Solutions
- Update Knowledge Plugin: Check if a newer version of
@elizaos/plugin-knowledgeis available that's compatible with the current AI SDK - Fix Prompt Construction: Update the knowledge plugin to construct prompts in the correct format
- 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
- Create a character configuration with
@elizaos/plugin-knowledgein plugins array - Add knowledge documents to the character configuration
- Set
CTX_KNOWLEDGE_ENABLED=truein environment - Start the ElizaOS agent (LOG_LEVEL=debug elizaos start --character test.json )
- Observe the error during document processing
Related Issues
This appears to be related to the AI SDK prompt validation changes mentioned in:
- Vercel AI SDK Discussion #5833
- AI SDK error documentation for
AI_InvalidPromptError