claude-agent-sdk-python
claude-agent-sdk-python copied to clipboard
SYSTEM PROMPT ISSUES
\n is allowed in system_prompt, I just checked. Avoid having any commented lines between agent options arguments.
same issue
Maybe you can use the monkey patch by claude to reslove the problem temporarily.
Indeed, prompt.replace("\n", "") can also solve the problem. Apart from line breaks, I haven’t encountered other cases so far.
from claude_agent_sdk._internal.transport.subprocess_cli import SubprocessCLITransport
_original_build_command = SubprocessCLITransport._build_command
def _patched_build_command(self):
cmd = _original_build_command(self)
# find and replace --system-prompt
if "--system-prompt" in cmd:
idx = cmd.index("--system-prompt")
if idx + 1 < len(cmd):
system_prompt = cmd[idx + 1]
# tempfile
if "\n" in system_prompt:
temp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, encoding="utf-8")
temp_file.write(system_prompt)
temp_file.close()
cmd[idx + 1] = f"@{temp_file.name}"
if "--append-system-prompt" in cmd:
idx = cmd.index("--append-system-prompt")
if idx + 1 < len(cmd):
append_prompt = cmd[idx + 1]
if "\n" in append_prompt:
temp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, encoding="utf-8")
temp_file.write(append_prompt)
temp_file.close()
cmd[idx + 1] = f"@{temp_file.name}"
return cmd
SubprocessCLITransport._build_command = _patched_build_command
print("[Workaround] Applied monkey patch to fix multi-line system_prompt issue")