skills/dspy-citations/SKILL.md
Use when you need structured source attribution in AI responses — verifiable citations that link claims to specific passages in source documents. Common scenarios - RAG with citation extraction, grounded answers with document references, legal or compliance use cases requiring source proof, building AI that cites its sources, or verifying which document an answer came from. Related - ai-stopping-hallucinations, ai-searching-docs, dspy-retrieval. Also used for dspy.experimental.Citations, dspy.experimental.Document, cite sources in DSPy, structured citations, source attribution, verify which document answer came from, RAG with citations, grounded answers with references, citation extraction, Anthropic Citations API DSPy, cited_text, document_index, adapt_to_native_lm_feature, parse_lm_response citations, streaming citations.
npx skillsauth add lebsral/dspy-programming-not-prompting-lms-skills dspy-citationsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Guide the user through adding structured citations to DSPy outputs so AI answers can be traced back to specific source passages.
dspy.experimental.Citations provides structured source attribution for LM outputs. Instead of inline quotes, you get machine-readable citation objects that identify exactly which passage from which document supports each claim. Works natively with Anthropic's Citations API and falls back to prompt-based extraction for other providers.
| Use Citations when... | Use something else when... | |----------------------|----------------------------| | You need to verify which document supports a claim | Simple RAG where inline quotes are enough | | Legal/compliance requires source traceability | Output does not reference source material | | Users need clickable references back to source docs | You only have one source document | | You want machine-readable citation metadata | Human-readable quotes in text are sufficient | | Building fact-checking or grounding verification | The task is creative generation (no sources) |
Create Document objects from your source material:
import dspy
from dspy.experimental import Citations, Document
lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-sonnet-4-5-20250929", etc.
dspy.configure(lm=lm)
# Create documents from your sources
documents = [
Document(
text="DSPy is a framework for programming language models. It replaces prompting with composable modules that can be optimized.",
title="DSPy Overview",
source_id="doc-001",
),
Document(
text="MIPROv2 is the most powerful DSPy optimizer. It jointly optimizes instructions and few-shot demonstrations.",
title="Optimizers Guide",
source_id="doc-002",
),
]
class CitedQA(dspy.Signature):
"""Answer the question using the provided context. Cite your sources."""
context: list[str] = dspy.InputField(desc="source documents")
question: str = dspy.InputField()
answer: str = dspy.OutputField()
citations: Citations = dspy.OutputField(desc="structured citations for claims in the answer")
qa = dspy.ChainOfThought(CitedQA)
# Format documents as context strings
context = [f"[{doc.source_id}] {doc.title}: {doc.text}" for doc in documents]
result = qa(
context=context,
question="What is the most powerful DSPy optimizer?",
)
print(result.answer)
# "MIPROv2 is the most powerful DSPy optimizer..."
for citation in result.citations:
print(f" Cited: '{citation.cited_text}' from document {citation.document_index}")
For Anthropic models, enable native citation support for higher accuracy:
lm = dspy.LM("anthropic/claude-sonnet-4-5-20250929")
dspy.configure(lm=lm)
# Enable native citations via adapter feature
dspy.configure(
lm=lm,
adapter=dspy.ChatAdapter(
adapt_to_native_lm_feature=["citations"],
),
)
# Now Citations output uses Anthropic's built-in citation extraction
# instead of prompt-based parsing -- higher accuracy, structured response
result = qa(
context=context,
question="How does DSPy replace prompting?",
)
Native mode advantages:
cited_text exactly matches source text (character-level accuracy)document_index reliably maps to the input document list# Each citation has these fields
for citation in result.citations:
print(f"Cited text: {citation.cited_text}")
print(f"Document index: {citation.document_index}")
print(f"Start char: {citation.start}")
print(f"End char: {citation.end}")
# Validate that cited text exists in the source document
for citation in result.citations:
source_doc = documents[citation.document_index]
if citation.cited_text in source_doc.text:
print(f"VALID: Citation found in {source_doc.title}")
else:
print(f"INVALID: Citation not found in source")
Stream answers while accumulating citations:
from dspy.streaming import streamify, StreamListener
qa = dspy.ChainOfThought(CitedQA)
answer_listener = StreamListener(signature_field_name="answer")
streaming_qa = streamify(qa, stream_listeners=[answer_listener])
async for chunk in streaming_qa(context=context, question="..."):
if hasattr(chunk, "answer"):
print(chunk.answer, end="", flush=True)
elif isinstance(chunk, dspy.Prediction):
# Citations are available in the final prediction
for citation in chunk.citations:
print(f"\n[{citation.document_index}] {citation.cited_text}")
For providers without native citation support, use prompt engineering:
class CitedAnswer(dspy.Signature):
"""Answer using ONLY information from the provided documents.
For each claim, include [doc_N] inline where N is the document number."""
context: list[str] = dspy.InputField(desc="numbered source documents")
question: str = dspy.InputField()
answer: str = dspy.OutputField(desc="answer with [doc_N] inline citations")
# Number your documents explicitly
numbered_context = [
f"[doc_{i}] {doc.title}: {doc.text}"
for i, doc in enumerate(documents)
]
qa = dspy.ChainOfThought(CitedAnswer)
result = qa(context=numbered_context, question="...")
# Parse [doc_N] references from the answer text
Citations type import. You must import from dspy.experimental -- it is not in the main dspy namespace. Use from dspy.experimental import Citations, Document.adapt_to_native_lm_feature=["citations"] with OpenAI, it silently falls back to prompt-based parsing which may be less accurate.document_index in citations maps to the position in the context list. If you reorder documents, indices change. Always use the index to look up the source, do not hardcode.dspy.experimental and may change between versions. Pin your DSPy version in production.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/ai-stopping-hallucinations/ai-searching-docs/dspy-retrieval/dspy-streaming/ai-do if you do not have it -- it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-dotools
See what is happening during optimizer.compile() instead of waiting blind. Use when you want to watch optimization progress, see scores as they come in, know if your optimizer is working, check if optimization is stuck, understand why optimization is taking too long, get live progress during compile, monitor convergence, detect overfitting during optimization, interpret optimization results, or pick the right tool for watching optimization. Also used for optimizer progress bar, is my optimizer doing anything, optimization seems stuck, how long will optimization take, watch GEPA run, watch MIPROv2 run, live optimization dashboard, optimizer not improving, scores not going up, optimization taking forever, see what optimizer is doing, debug slow optimization, optimization visibility, optimizer metrics, track compile progress, optimization observability.
testing
Use when you want the highest-quality prompt optimization DSPy offers — jointly optimizes instructions and few-shot demos, with auto=light/medium/heavy presets. Common scenarios - you want the best possible accuracy from prompt optimization, jointly tuning instructions and few-shot demonstrations, using auto presets for different compute budgets, or when COPRO or BootstrapFewShot alone are not reaching your accuracy target. Related - ai-improving-accuracy, dspy-copro, dspy-bootstrap-few-shot. Also used for dspy.MIPROv2, best DSPy optimizer, highest quality optimization, auto=light medium heavy, joint instruction and demo optimization, most powerful prompt optimizer, MIPROv2 vs COPRO vs BootstrapFewShot, which optimizer should I use, state of the art prompt optimization, when to use MIPROv2, optimize both instructions and examples, heavy optimization for production, best optimizer for accuracy.
testing
Use LangWatch for DSPy auto-tracing and real-time optimizer progress. Use when you want to set up LangWatch, langwatch.dspy.init, auto-tracing DSPy, real-time optimization dashboard, optimizer progress tracking, app.langwatch.ai, or DSPy optimizer dashboard. Also used for langwatch setup, pip install langwatch, langwatch trace, optimizer progress, real-time optimization, watch optimizer run, LangWatch self-hosted, langwatch docker, langwatch vs langtrace, langwatch autotrack_dspy.
data-ai
Use when you want to optimize instructions without few-shot examples — a lightweight alternative to COPRO when you do not have or do not want to use demonstrations. Common scenarios - optimizing instructions when you do not have or do not want to use few-shot demonstrations, lightweight instruction search as a first step, tasks where examples in the prompt confuse the model, or when you want fast instruction optimization without the cost of COPRO. Related - ai-improving-accuracy, dspy-copro, dspy-miprov2. Also used for dspy.GEPA, instruction optimization without demos, lightweight prompt optimization, optimize instructions only, no few-shot examples needed, GEPA vs COPRO, quick instruction search, when demonstrations hurt performance, zero-shot optimization, instruction-only optimizer, simplest instruction tuner, fast prompt optimization, skip few-shot and just tune instructions, optimize Pydantic field descriptions, GEPA structured output, GEPA does not optimize field desc.