> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 更新对话变量

> **适用于**：Chatflow、聊天助手、Agent。

更新特定会话变量的值。值必须与预期类型匹配。



## OpenAPI

````yaml /zh/api-reference/openapi_service.json put /conversations/{conversation_id}/variables/{variable_id}
openapi: 3.0.1
info:
  title: Dify 服务 API
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Dify 服务 API 的基础 URL。自部署时，替换为你的 API 基础 URL。
    variables:
      api_base_url:
        default: api.dify.ai/v1
        description: API 基础 URL 的主机与路径，不含 `https://` 前缀。
security:
  - ApiKeyAuth: []
tags:
  - name: 对话消息
    description: 与聊天消息和交互相关的操作。
  - name: 文件操作
    description: 文件上传和预览操作。
  - name: 终端用户
    description: 终端用户信息相关操作。
  - name: 消息反馈
    description: 用户反馈操作。
  - name: 会话管理
    description: 与管理会话相关的操作。
  - name: 语音与文字转换
    description: 文字转语音和语音转文字操作。
  - name: 应用配置
    description: 获取应用设置和信息的操作。
  - name: 标注管理
    description: 与管理标注直接回复相关的操作。
  - name: 人工介入
    description: 暂停等待人工输入的工作流恢复操作。
  - name: 工作流运行
    description: 用于执行和管理工作流的操作。
  - name: 文本生成消息
    description: 文本生成相关操作。
  - name: 知识库
    description: 用于管理知识库的操作，包括创建、配置和检索。
  - name: 文档
    description: 用于在知识库中创建、更新和管理文档的操作。
  - name: 分段
    description: 用于管理分段和子分段的操作。
  - name: 元数据
    description: 用于管理知识库元数据字段和文档元数据值的操作。
  - name: 标签
    description: 用于管理知识库标签和标签绑定的操作。
  - name: 模型
    description: 用于获取可用模型的操作。
  - name: 知识流水线
    description: 用于管理和运行知识流水线的操作，包括数据源插件和流水线执行。
paths:
  /conversations/{conversation_id}/variables/{variable_id}:
    put:
      tags:
        - 会话管理
      summary: 更新对话变量
      description: |-
        **适用于**：Chatflow、聊天助手、Agent。

        更新特定会话变量的值。值必须与预期类型匹配。
      operationId: updateChatConversationVariableZh
      parameters:
        - name: conversation_id
          in: path
          required: true
          description: 会话 ID。
          schema:
            type: string
            format: uuid
        - name: variable_id
          in: path
          required: true
          description: Variable ID.
          schema:
            type: string
            format: uuid
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ConversationVariableUpdateRequest'
            examples:
              updateStringVariable:
                summary: 请求示例
                value:
                  value: new value
                  user: abc-123
      responses:
        '200':
          description: 变量更新成功。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ConversationVariableItem'
              examples:
                updatedVariable:
                  summary: 响应示例
                  value:
                    id: a1b2c3d4-5678-90ab-cdef-1234567890ab
                    name: user_preference
                    value_type: string
                    value: new value
                    description: 用户偏好设置
                    created_at: 1705407629
                    updated_at: 1705411229
        '400':
          description: |-
            - `not_chat_app` : 应用模式与 API 路由不匹配。
            - `bad_request` : 变量值类型不匹配。
          content:
            application/json:
              examples:
                not_chat_app:
                  summary: not_chat_app
                  value:
                    status: 400
                    code: not_chat_app
                    message: Please check if your app mode matches the right API route.
                type_mismatch:
                  summary: bad_request
                  value:
                    status: 400
                    code: bad_request
                    message: 'Value type mismatch: expected string, got integer.'
        '404':
          description: |-
            - `not_found` : 会话不存在。
            - `not_found` : 会话变量不存在。
          content:
            application/json:
              examples:
                conversation_not_exists:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Conversation Not Exists.
                variable_not_exists:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Conversation Variable Not Exists.
components:
  schemas:
    ConversationVariableUpdateRequest:
      type: object
      description: 更新会话变量的请求体。
      required:
        - value
      properties:
        value:
          description: 变量的新值。必须与变量的预期类型匹配。
        user:
          type: string
          description: 用户标识符。
    ConversationVariableItem:
      type: object
      properties:
        id:
          type: string
          format: uuid
          description: Variable ID.
        name:
          type: string
          description: 变量名称。
        value_type:
          type: string
          description: >-
            变量值类型。可选值：`string`、`number`、`object`、`secret`、`file`、`boolean`、`array[any]`、`array[string]`、`array[number]`、`array[object]`、`array[file]`、`array[boolean]`。
        value:
          type: string
          description: 变量值（复杂类型可以是 JSON 字符串）。
        description:
          type: string
          description: 变量描述。
        created_at:
          type: integer
          format: int64
          description: 创建时间戳。
        updated_at:
          type: integer
          format: int64
          description: 最后更新时间戳。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 认证。对于所有 API 请求，请在 `Authorization` HTTP 头中包含您的 API Key，并加上
        `Bearer ` 前缀。示例：`Authorization: Bearer {API_KEY}`。**强烈建议将 API Key
        存储在服务端，不要在客户端共享或存储，以避免 API Key 泄漏导致严重后果。**缺少或无效的 API Key 会返回 HTTP
        `401`，错误码为 `unauthorized`。

````