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

# 使用 Python Agent SDK 提取结构化 JSON

> 使用 Python Agent SDK 构建范围明确的本地工具循环，并通过 Phaseo Gateway 返回严格 JSON。

当 Python 工作流需要以下内容时，可使用此示例：

* 一个本地查询工具
* 一个范围明确的 Agent 循环
* 一种严格的 JSON 输出结构

## 1. 准备 SDK

<Note type="warning">Python Agent SDK 包尚未发布到 PyPI。请参阅[从源代码安装说明](../sdk-reference/python/agent-sdk-installation.mdx)。</Note>

## 2. 定义本地工具

```python theme={null}
from phaseo_agent import AgentTool, define_tool

lookup_policy = define_tool(
    AgentTool(
        id="lookup-policy",
        description="Look up one internal policy by slug.",
        parameters={
            "type": "object",
            "properties": {
                "slug": {"type": "string"},
            },
            "required": ["slug"],
            "additionalProperties": False,
        },
        execute=lambda input, ctx: {
            "slug": input["slug"],
            "summary": "Presets centralize routing, prompts, and caching defaults.",
        },
    )
)
```

## 3. 创建 Agent

```python theme={null}
from phaseo_agent import create_agent

agent = create_agent(
    {
        "id": "policy-extractor",
        "model": "openai/gpt-5.4-nano",
        "instructions": "Use tools when needed and return concise JSON.",
        "tools": [lookup_policy],
        "parse_output": lambda text: text,
    }
)
```

## 4. 通过网关客户端运行 Agent

```python theme={null}
from phaseo_agent import create_gateway_agent_client

result = agent.run(
    input="Look up presets and return JSON with keys summary and recommendation.",
    client=create_gateway_agent_client(),
)

print(result.output)
```

## 5. 适用场景

* 由一个 Python 工作进程负责编排
* 工具接口位于本地且行为确定
* 你想先获得严格输出，再构建更大的编排层

## 相关内容

* [Python Agent SDK](../sdk-reference/python/agent-sdk.mdx)
* [使用 Python 输出结构化 JSON](./sdk-python-presets-and-json.mdx)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.