Repository navigation
Description
Summary
SubprocessCLITransport._build_command at src/claude_agent_sdk/_internal/transport/subprocess_cli.py:209-220 only accepts system_prompt as one of:
strNone{"type": "file", "path": ...}(SystemPromptFile){"type": "preset", "append": ...}(SystemPromptPreset)
When a caller passes the list-of-content-blocks form (which is valid at the upstream Anthropic Messages API per the system parameter docs), the transport raises:
AttributeError: 'list' object has no attribute 'get'
File ".../subprocess_cli.py", line 215, in _build_command
if sp.get("type") == "file":
before any model call. The crash happens at command-build time; the user-facing ClaudeAgentOptions(system_prompt=...) doesn't validate the shape, so the failure is delayed and noisy.
Use case
Anthropic's prompt_caching documentation specifically recommends passing system as a list of content blocks so cache_control can be attached to a trailing block and yield layer-independent caching of role vs. injected reference material. We use this pattern to layer (1) a stable role system prompt with (2) per-call retrieval-style skill content. The cache_proxy in our stack handles the list form correctly; only the SDK's bundled CLI transport blocks it.
Worked example we hit today (paid validation, $0.46 burned before crash):
options = ClaudeAgentOptions(
system_prompt=[
{"type": "text", "text": role_system_prompt},
{"type": "text", "text": injected_skill_content},
],
...
)
async for msg in query(prompt=user_prompt, options=options):
...
# AttributeError at SubprocessCLITransport._build_commandSuggested fix
_build_command could:
-
Detect
isinstance(sp, list)and serialize via a new flag, e.g.--system-prompt-json <path>pointing at a temp file containing the JSON-encoded list. The bundledclaudeCLI would parse that and forward as-is to the API. -
Alternatively, accept the list form on the existing
--system-prompt-filepath with content auto-detection (string vs. JSON array).
Either keeps the wire format Anthropic-compatible and avoids the silent client-side flatten that today's only workaround (concatenate to string) forces.
Workaround we're using
We collapse the list to a single concatenated string at the runtime layer and document the cost (loss of layer-independent caching) in our ADR. This regresses option 4 ("structured 2-block") to option 3 ("concatenated string") in our chosen design. We'd revert the regression as soon as the SDK forwards list-form.
Reproduction
claude-agent-sdk 0.1.66, bundled CLI 2.1.119:
from claude_agent_sdk import ClaudeAgentOptions, query
options = ClaudeAgentOptions(
system_prompt=[{"type": "text", "text": "hello"}, {"type": "text", "text": "world"}],
model="claude-opus-4-7",
)
async for msg in query(prompt="hi", options=options):
print(msg)
# AttributeError: 'list' object has no attribute 'get'Related
- Upstream Anthropic API reference:
systemparameter acceptsstring | content_block[]per Messages API docs. - Caching guidance: prompt caching recommends placing
cache_controlon the LAST block of each cacheable layer.
Activity
zion-off commentedon May 1, 2026
i have a pr open for this if anyone from the team would like to take a look https://github.com/anthropics/claude-agent-sdk-python/pull/900/changes
thanks !! : )
fix: accept list-form system_prompt in ClaudeAgentOptions (anthropics…
pavle-Scalably commentedon Jun 21, 2026
I hit this exact wall using list-form `system_prompt` for prompt caching (putting `cache_control` on a trailing block). Happy to take it.
Plan: handle `isinstance(sp, list)` in `_build_command` by writing the content-block list to a temp JSON file and passing it through `--system-prompt-file` with content auto-detection (string vs. JSON array), so the wire format stays API-compatible and we avoid the lossy concat-to-string workaround. Will add tests covering the str / file / preset / list forms.
Will open a PR shortly.
pavle-Scalably commentedon Jun 21, 2026
Spent some time on this and the clean fix I had in mind doesn't actually hold up, so I want to flag what I found before anyone (me included) sinks time into a PR that looks right but isn't.
The obvious approach is to serialize the list to JSON and pass it through the existing
--system-prompt-fileflag. But that flag reads the file as plain text and uses the contents verbatim as the system prompt (it was added in #591 to get around the argmax limit on a large text prompt). Write a JSON array there and the CLI sends the literal JSON source as the system prompt - thecache_controlblocks are never honored, so the whole reason for using the list form silently doesn't work.I also checked whether the
initializerequest could carry it structurally:_internal/client.pyonly pullsexclude_dynamic_sectionsout of a preset system prompt, the content itself isn't forwarded. So as far as I can tell there's no existing channel that gets list-formsystemblocks (withcache_control) through to the Messages API.There's a second wrinkle on the typing side: the SDK's
TextBlock/ThinkingBlock/etc. are inbound parsing dataclasses -TextBlockis justtext: str, notypediscriminator and nocache_controlfield - so they can't represent the cached-block payload even if the transport accepted it.So this isn't a
_build_commandpatch, it's a missing capability. A real fix needs a structured channel - either a new CLI flag that forwards a JSONsystemarray straight to the API, or aninitializefield for it. Both touch the bundled CLI, so it probably needs a maintainer call on the preferred shape before any SDK-side code. Happy to help build the SDK side once there's a direction. Wanted to save the next person the detour.fix: reject unsupported system_prompt types with a clear error