> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# INVALID_PROMPT_INPUT

当 [提示模板](https://github.com/langchain-ai/langchain/blob/v0.3/docs/docs/concepts/prompt_templates.mdx) 接收到缺失或无效的输入变量时发生。

一种可能意外触发此错误的情况是，如果你直接将 JSON 对象添加到提示模板中：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";

const prompt = PromptTemplate.fromTemplate(`你是一个有用的助手。

以下是你应该回应的示例：

{
  "firstName": "John",
  "lastName": "Doe",
  "age": 21
}

现在，请回答以下问题：

{question}`);
```

你可能会认为上述提示模板应该只需要一个名为 question 的输入键，但 JSON 对象会被解释为一个额外的变量，因为花括号（`{`）没有被转义，而应该在前面加上第二个花括号，像这样：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";

const prompt = PromptTemplate.fromTemplate(`你是一个有用的助手。

以下是你应该回应的示例：

{{
  "firstName": "John",
  "lastName": "Doe",
  "age": 21
}}

现在，请回答以下问题：

{question}`);
```

## 故障排除

要解决此错误，你可以：

1. 检查提示模板的正确性。使用 f-string 格式时，确保正确转义花括号：
   * 在 f-string 中使用 `{{` 表示单个花括号
   * 在 f-string 中使用 `{{{{` 表示双花括号
2. 使用 `MessagesPlaceholder` 组件时，确认你传递的是消息数组或类似消息的对象。如果使用简写元组，请将变量名用花括号包裹，如 `["placeholder", "{messages}"]`
3. 通过使用 [LangSmith](/langsmith/home) 或日志记录来调试，检查提示模板的实际输入，以验证它们是否符合预期
4. 如果从 LangChain [Prompt Hub](https://smith.langchain.com/prompts) 获取提示，请使用示例输入隔离并测试提示，以确保其按预期工作

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\langchain\errors\INVALID_PROMPT_INPUT.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
