DS DevShelfHub Projects · AI tools
Tutorials / CrewAI / Reference / Classes / StringKnowledgeSource
Class knowledge

StringKnowledgeSource: Reference Guide

By DevShelfHub

Use a raw string as a knowledge source — fastest path for prototyping or test fixtures.

See the CrewAI API reference index, CrewAI introduction, and core concepts for surrounding context.

What is StringKnowledgeSource?

StringKnowledgeSource wraps literal Python text so CrewAI can chunk, embed, and retrieve it like any other knowledge adapter. It shines when the corpus is tiny and authoritative — a refund blurb, an SLA paragraph, or a pytest fixture that should never hit disk. Because the bytes live in your source tree, CI can diff policy changes alongside code reviews.

The tradeoff is operational: large strings bloat imports, defeat translation workflows, and re-embed on every cold start if your loader eagerly indexes. Once content exceeds a few kilobytes, migrate to TextFileKnowledgeSource or PDFKnowledgeSource so editors can iterate without redeploying Python. Also remember knowledge text becomes part of prompts — never embed API keys or customer payloads in StringKnowledgeSource constants.

For multi-tenant SaaS, prefer per-tenant files or database-backed BaseKnowledgeSource implementations instead of baking tenant data into strings checked into git.

When to Use

Unit tests, demos, micro-policies, and bootstrap knowledge before you wire a real CMS.

Use Cases

  • Inline policies
  • Test fixtures
  • Conference demo scripts
  • Golden answers for eval harnesses

Key Features

  • Zero filesystem IO
  • Identical retrieval pipeline as file-backed sources
  • Great for CI fixtures

When NOT to Use

Large manuals, regulated document retention, or frequently edited content owned by non-engineers.

Notes

Embedding churn

Changing the string invalidates prior vectors for that source. For volatile text, store version metadata or move to a file-backed source so ops can refresh embeddings without code deploys.

Secrets and PII

Anything in content can surface in retrieval snippets. Treat StringKnowledgeSource like a committed config file — redact before merge and scan with secret linters.

Size guidance

If content spans hundreds of lines, chunking still runs but developer ergonomics collapse. Split into Markdown files and use TextFileKnowledgeSource for editability.

Multi-language

Mixing languages in one blob hurts embedding quality. Prefer separate sources per locale so recall stays precise.

Import

python
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

Key Parameters

Parameter Type Default Purpose
content str The knowledge text.

Code Examples

Inline policy snippet

python
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

policy = StringKnowledgeSource(content='Refunds: 30-day window. Excludes digital licenses.')

Attach to a specialist agent

python
from crewai import Agent
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

kb = StringKnowledgeSource(content='Tone: concise, US English, no exclamation marks.')

agent = Agent(
    role='Support drafter',
    goal='Answer tickets',
    backstory='You follow the tone guide exactly.',
    knowledge_sources=[kb],
)

Crew-wide micro-policy

python
from crewai import Crew, Agent, Task
from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

hr = StringKnowledgeSource(content='PTO: request 10 business days in advance.')

crew = Crew(agents=[agent], tasks=[task], knowledge_sources=[hr])

Common Mistakes

❌ Dumping entire JSON exports into a string constant

✅ Use JSONKnowledgeSource or stream rows through a custom BaseKnowledgeSource.

❌ Checking customer-specific data into git via StringKnowledgeSource

✅ Load tenant corpora from secure storage at runtime.

StringKnowledgeSource FAQ

What is StringKnowledgeSource in CrewAI?

Use a raw string as a knowledge source — fastest path for prototyping or test fixtures. StringKnowledgeSource wraps literal Python text so CrewAI can chunk, embed, and retrieve it like any other knowledge adapter. It shines when the corpus is tiny and authoritative — a refund blurb, an SLA paragraph, or a pytest fixture that should never hit disk. Because the bytes live in your source tree, CI can diff policy changes alongside code reviews. The tradeoff is operational: large strings bloat imports, defeat translation workflows, and re-embed on every cold start i…

Which package defines the CrewAI class StringKnowledgeSource?

DevShelfHub maps StringKnowledgeSource to Python module crewai.knowledge.source (package path crewai.knowledge.source in this reference). Pin your installed crewai version and match imports to the snippet on this page.

When should I use StringKnowledgeSource?

Unit tests, demos, micro-policies, and bootstrap knowledge before you wire a real CMS.

When should I avoid using StringKnowledgeSource?

Large manuals, regulated document retention, or frequently edited content owned by non-engineers.

How do I import StringKnowledgeSource in Python?

from crewai.knowledge.source.string_knowledge_source import StringKnowledgeSource

Where can I explore more CrewAI API reference pages?

Open the CrewAI API reference index on DevShelfHub to search 58 classes, 30 methods, and 16 decorators, each with runnable examples, parameters, common mistakes, and cross-links.