跳到内容

可配置的标头

LangGraph 允许在运行时配置,以动态修改代理行为和权限。当使用 LangGraph 平台 时,你可以在请求体 (config) 或特定的请求标头中传递此配置。这使得可以根据用户身份或其他请求数据进行调整。

为了隐私,请通过 langgraph.json 文件中的 http.configurable_headers 部分来控制哪些标头被传递到运行时配置中。

以下是如何自定义包含和排除的标头

{
  "http": {
    "configurable_headers": {
      "include": ["x-user-id", "x-organization-id", "my-prefix-*"],
      "exclude": ["authorization", "x-api-key"]
    }
  }
}

includeexclude 列表接受精确的标头名称或使用 * 匹配任意数量字符的模式。为了您的安全,不支持其他正则表达式模式。

在图中应用

你可以使用任何节点的 config 参数在图中访问包含的标头。

def my_node(state, config):
  organization_id = config["configurable"].get("x-organization-id")
  ...

或者通过从上下文中获取(在工具中或在其他嵌套函数中有用)。

from langgraph.config import get_config

def search_everything(query: str):
  organization_id = get_config()["configurable"].get("x-organization-id")
  ...

你甚至可以使用此功能来动态编译图。

# my_graph.py.
import contextlib

@contextlib.asynccontextmanager
async def generate_agent(config):
  organization_id = config["configurable"].get("x-organization-id")
  if organization_id == "org1":
    graph = ...
    yield graph
  else:
    graph = ...
    yield graph
{
  "graphs": {"agent": "my_grph.py:generate_agent"}
}

选择退出可配置标头

如果你希望选择退出可配置标头,只需在 exclude 列表中设置一个通配符模式即可

{
  "http": {
    "configurable_headers": {
      "exclude": ["*"]
    }
  }
}

这将排除所有标头被添加到你的运行配置中。

请注意,排除项优先于包含项。