Explains DSPy's ChatAdapter: how it formats signatures into chat messages with field delimiters, parses LM responses, and falls back to JSONAdapter. Debug formatting issues, customize prompt rendering, enable native function calling, or generate fine-tuning data.
Ships in dspy-api-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Before diving into adapter internals, clarify:
If you just need to pick the right adapter, start with /dspy-adapters instead -- it covers the decision between ChatAdapter, JSONAdapter, TwoStepAdapter, and XMLAdapter.
ChatAdapter is the default adapter in DSPy. Every time a module calls an LM, ChatAdapter handles two jobs:
[[ ## field_name ## ]] delimitersYou never call it directly -- DSPy uses it behind the scenes. But understanding its internals helps you debug formatting issues and customize behavior.
dspy.ChatAdapter(
callbacks=None, # list[BaseCallback] | None
use_native_function_calling=False, # bool
native_response_types=None, # list[type] | None
use_json_adapter_fallback=True, # bool
parallel_tool_calls=None, # bool | None
)
| Parameter | Type | Default | What it controls |
|---|---|---|---|
callbacks | list[BaseCallback] | None | None | Callback hooks executed during format/parse |
use_native_function_calling | bool | False | Use provider-native function calling for structured output |
native_response_types | list[type] | None | None | Output field types handled by native LM features instead of text parsing |
use_json_adapter_fallback | bool | True | Automatically retry with JSONAdapter when parsing fails |
parallel_tool_calls | bool | None | None | Enable provider-side parallel tool-call generation when native function calling is active |
ChatAdapter converts a DSPy call into a multi-turn message list:
System message: Task instructions from the signature docstring
+ field structure showing expected input/output format
+ output type hints and constraints
Demo messages: For each few-shot demo:
User message: input fields with [[ ## field ## ]] headers
Assistant message: output fields with headers + [[ ## completed ## ]]
History messages: If dspy.History is used, prior conversation turns
User message: Current input fields with headers
+ output format reminder (for long conversations)
ChatAdapter marks each field with header delimiters:
[[ ## question ## ]]
What is the capital of France?
[[ ## answer ## ]]
Paris
[[ ## completed ## ]]
The [[ ## completed ## ]] marker signals that the LM has finished all output fields. This is how parse() knows where output ends.
Use dspy.inspect_history() to see the exact messages ChatAdapter builds:
import dspy
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini")) # or "anthropic/claude-sonnet-4-5-20250929", etc.
program = dspy.ChainOfThought("question -> answer")
result = program(question="What is DSPy?")
# See the full prompt and response
dspy.inspect_history(n=1)
When the LM responds, parse():
[[ ## field_name ## ]] headersparse_value() to cast each value to its declared Python typeIf any step fails, the adapter raises AdapterParseError -- which triggers the JSON fallback (if enabled).
By default, ChatAdapter automatically retries with JSONAdapter when parsing fails:
ChatAdapter.parse() succeeds? -> Return result
fails? -> Is it an LMError?
Yes -> Re-raise (cannot fix by reformatting)
No -> Retry entire call with JSONAdapter
This means most parse failures self-heal without intervention. To observe when fallback triggers, enable debug logging or check dspy.inspect_history() for duplicate calls.
To disable the fallback:
adapter = dspy.ChatAdapter(use_json_adapter_fallback=False)
dspy.configure(lm=lm, adapter=adapter)
# Now parse failures raise AdapterParseError immediately
Some providers (OpenAI, Anthropic) support native structured output via function calling. ChatAdapter can use this instead of text-based field delimiters:
adapter = dspy.ChatAdapter(use_native_function_calling=True)
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"), adapter=adapter)
# Output fields are now enforced via the provider's function calling API
# rather than text delimiters in the prompt
Use native_response_types to limit which output types use native features:
from pydantic import BaseModel
class StructuredResult(BaseModel):
category: str
confidence: float
# Only use native function calling for Pydantic output types
adapter = dspy.ChatAdapter(
use_native_function_calling=True,
native_response_types=[BaseModel],
)
ChatAdapter formats demos as user/assistant message pairs. Demos come in two flavors:
Complete demos (all fields present):
User: [[ ## question ## ]]
What color is the sky?
Assistant: [[ ## answer ## ]]
Blue
[[ ## completed ## ]]
Incomplete demos (some fields missing -- common during bootstrapping):
User: This is an example of the task, though some input or output
fields are not supplied.
[[ ## question ## ]]
What color is the sky?
Assistant: [[ ## answer ## ]]
Blue
[[ ## completed ## ]]
The prefix on incomplete demos tells the LM not to infer missing fields from incomplete examples.
ChatAdapter handles dspy.History fields by converting them into alternating user/assistant message pairs inserted before the current input:
import dspy
class Chatbot(dspy.Module):
def __init__(self):
self.respond = dspy.Predict("history: dspy.History, question -> response")
def forward(self, history, question):
return self.respond(history=history, question=question)
# History becomes prior message pairs in the formatted prompt
history = dspy.History(
messages=[
{"role": "user", "content": "Hi there"},
{"role": "assistant", "content": "Hello! How can I help?"},
]
)
ChatAdapter can produce OpenAI-compatible fine-tuning data from your DSPy programs:
adapter = dspy.ChatAdapter()
# Generate fine-tuning format for a single example
finetune_data = adapter.format_finetune_data(
signature=my_signature,
demos=my_demos,
inputs={"question": "What is DSPy?"},
outputs={"answer": "A framework for programming LMs"},
)
# Returns: {"messages": [{"role": "system", ...}, {"role": "user", ...}, {"role": "assistant", ...}]}
This is useful when you want to fine-tune a model on the exact prompt format DSPy uses, ensuring the fine-tuned model responds in a way ChatAdapter can parse reliably.
| Aspect | ChatAdapter | JSONAdapter | TwoStepAdapter | XMLAdapter |
|---|---|---|---|---|
| Delimiter style | [[ ## field ## ]] headers | JSON object keys | Natural language (step 1) + ChatAdapter (step 2) | <field>...</field> XML tags |
| Parse resilience | Falls back to JSONAdapter | json_repair library | Delegated to extraction LM | Falls back to JSONAdapter |
| Native structured output | Optional (use_native_function_calling) | On by default | N/A | No |
| LM calls per prediction | 1 | 1 | 2 (main + extraction) | 1 |
| Best for | General use, most models | Reliable structured output, complex Pydantic types | Reasoning models (o1, o3) | Models that respond well to XML |
BamlAdapter (
dspy.adapters.BamlAdapter) exists in the source (dspy/adapters/baml_adapter.py) but has no official docs page and is undocumented — treat as experimental. Do not recommend it in production code.
JSONAdapterTwoStepAdapterXMLAdapterdspy.configure(lm=lm) already uses it. Only instantiate explicitly when you need to change a parameter like use_json_adapter_fallback=False or use_native_function_calling=True.use_native_function_calling=True for all providers. Not all providers support native function calling. OpenAI and Anthropic do; many local models and smaller providers do not. If the provider does not support it, the call fails. Check provider capabilities before enabling, or let ChatAdapter fall back to text-based delimiters.[[ ## field ## ]] format, ChatAdapter automatically retries with JSONAdapter. Before adding manual error handling or switching adapters, check dspy.inspect_history() to see if the fallback already succeeded silently.DSPyInstrumentor().instrument() after the adapter is configured and expects to see adapter details in traces. The adapter formats and parses happen inside the LM call. Instrumentation captures the LM call, but adapter internals (which delimiter style was used, whether fallback triggered) are not always visible in traces. Use dspy.inspect_history() for adapter-level debugging.[[ ## completed ## ]] when manually constructing few-shot demos. If you build demos by hand (not via optimization), omitting the completion marker causes the LM to keep generating past the expected output. Let DSPy handle demo formatting through BootstrapFewShot or LabeledFewShot rather than manually constructing demos with delimiters.Install any skill:
npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
/dspy-adapters/dspy-signatures/dspy-lminspect_history -- see /dspy-utilsformat_finetune_data -- see /ai-fine-tuning/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-doWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/dspy-api-skills:dspy-chatadapterfrom dspy-api-skills
npx claudepluginhub lebsral/dspy-programming-not-prompting-lms-skills --plugin dspy-api-skills39 skills in this plugin
/dspy-adaptersUse when you need to customize how DSPy formats prompts for a specific provider — switching from chat to completion format, forcing JSON output, or debugging prompt rendering issues. Common scenarios - debugging why your prompt looks wrong when sent to the model, switching from OpenAI to Anthropic and the formatting breaks, forcing the model to return valid JSON instead of markdown, working with completion-style models that do not support chat format, customizing system messages, or handling models that choke on structured output instructions. Related - ai-switching-models, ai-following-rules, ai-parsing-data. Also used for prompt template rendering, how DSPy builds the prompt, custom system message in DSPy, JSON mode not working, model ignores format instructions, switch from chat to completion API, dspy.ChatAdapter, dspy.JSONAdapter, prompt formatting issues, debug what DSPy sends to the model, dspy.XMLAdapter, XML output format./dspy-assertionsLegacy reference for dspy.Assert and dspy.Suggest, both removed in DSPy 3.x - use dspy.Refine or dspy.BestOfN instead. Use when maintaining a legacy DSPy 2.x codebase that uses dspy.Assert or dspy.Suggest, or migrating from Assert/Suggest to DSPy 3.x. Also used for dspy.Assert, dspy.Suggest, runtime validation for LLM output, retry on bad output, backtracking on constraint violation, guard rails in DSPy, migrate from Assert, migrate from Suggest, ImportError cannot import Assert, ImportError cannot import Suggest.Other plugins' skills for the same job