claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

SYSTEM PROMPT ISSUES

Open mbt1909432 opened this issue 2 months ago • 2 comments

Image when add '\n' to the system prompt ,the program cannot run Image

mbt1909432 avatar Oct 16 '25 08:10 mbt1909432

\n is allowed in system_prompt, I just checked. Avoid having any commented lines between agent options arguments.

arindam-giri avatar Oct 16 '25 12:10 arindam-giri

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")

QudaRulo avatar Oct 19 '25 13:10 QudaRulo