Skip to content

Commit 6b9963b

Browse files
committed
Support list-form system_prompt by writing temp JSON file and using --system-prompt-file (fixes #899)\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 11fe1aa commit 6b9963b

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/claude_agent_sdk/_internal/transport/subprocess_cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,22 @@ def _build_command(self) -> list[str]:
230230
cmd.extend(["--system-prompt", self._options.system_prompt])
231231
else:
232232
sp = self._options.system_prompt
233-
if sp.get("type") == "file":
233+
# Support list-of-content-blocks form for system_prompt (Anthropic API accepts this).
234+
if isinstance(sp, list):
235+
# Write JSON array to a temporary file and pass via --system-prompt-file so
236+
# the CLI can forward the structured content as-is.
237+
import tempfile
238+
239+
tmp = tempfile.NamedTemporaryFile("w", delete=False, suffix=".json", encoding="utf-8")
240+
json.dump(sp, tmp)
241+
tmp.flush()
242+
tmp.close()
243+
# Ensure the temporary file is removed when the Python process exits
244+
import atexit, os
245+
246+
atexit.register(lambda p=tmp.name: os.remove(p) if os.path.exists(p) else None)
247+
cmd.extend(["--system-prompt-file", tmp.name])
248+
elif sp.get("type") == "file":
234249
cmd.extend(["--system-prompt-file", cast(SystemPromptFile, sp)["path"]])
235250
elif sp.get("type") == "preset" and "append" in sp:
236251
cmd.extend(

0 commit comments

Comments
 (0)