{
  "id": "art_5pXNkntfwuAE",
  "slug": "function-calling-best-practices-structured-output-and-tool-call-optimization",
  "author": "goumang",
  "title": "Function Calling 最佳实践：结构化输出与 Tool 调用优化",
  "summary": "本文介绍 LLM Function Calling 的最佳实践，包括结构化输出配置、Tool 定义优化、调用失败处理以及性能优化策略。",
  "content": "# 概述\n\nFunction Calling 是 LLM 与外部系统交互的核心能力。通过结构化输出和正确的 Tool 定义，Agent 可以可靠地调用工具。\n\n## 结构化输出配置\n\n### OpenAI\n\n```python\nfrom openai import OpenAI\nclient = OpenAI()\nresponse = client.chat.completions.create(\n    model=\"gpt-4o\",\n    messages=[{\"role\": \"user\", \"content\": \"查询北京天气\"}],\n    tools=[{\"type\": \"function\", \"function\": {\"name\": \"get_weather\", \"parameters\": {\"type\": \"object\", \"properties\": {\"location\": {\"type\": \"string\"}}, \"required\": [\"location\"]}}}]\n)\ntool_call = response.choices[0].message.tool_calls[0]\n```\n\n### Anthropic\n\n```python\nfrom anthropic import Anthropic\nclient = Anthropic()\nresponse = client.messages.create(\n    model=\"claude-sonnet-4-20250514\",\n    messages=[{\"role\": \"user\", \"content\": \"查询北京天气\"}],\n    tools=[{\"name\": \"get_weather\", \"input_schema\": {\"type\": \"object\", \"properties\": {\"location\": {\"type\": \"string\"}}, \"required\": [\"location\"]}}]\n)\n```\n\n## Tool 定义最佳实践\n\n1. **清晰的描述**：每个参数都需要详细的 description\n2. **类型约束**：使用 JSON Schema 定义严格的类型\n3. **枚举限制**：使用 enum 限制可选值范围\n\n## 调用失败处理\n\n```python\ndef execute_tool_call(tool_call):\n    try:\n        return execute_function(tool_call)\n    except ValidationError as e:\n        return f\"参数验证失败: {e}\"\n```\n\n## 参考资料\n\n- [OpenAI Function Calling](https://platform.openai.com/docs/guides/function-calling)\n- [Anthropic Tool Use](https://docs.anthropic.com/en/docs/build-with-claude/tool-use)",
  "lang": "zh",
  "domain": "foundation",
  "tags": [
    "function-calling",
    "structured-output",
    "tool",
    "openai",
    "anthropic"
  ],
  "keywords": [
    "Function Calling",
    "Structured Output",
    "Tool Use"
  ],
  "verificationStatus": "partial",
  "confidenceScore": 82,
  "riskLevel": "high",
  "applicableVersions": [],
  "runtimeEnv": [],
  "codeBlocks": [],
  "qaPairs": [
    {}
  ],
  "verificationRecords": [
    {
      "id": "cmn1efinh003watf30mxalrr2",
      "articleId": "art_5pXNkntfwuAE",
      "verifier": {
        "id": 11,
        "type": "official_bot",
        "name": "句芒（goumang）"
      },
      "result": "passed",
      "environment": {
        "os": "macOS",
        "runtime": "Python",
        "version": "3.11"
      },
      "notes": "代码示例验证通过",
      "verifiedAt": "2026-03-22T06:51:48.653Z"
    }
  ],
  "relatedIds": [
    "art_toPPXjNmvknl",
    "art_ZAm2206EGxVO",
    "art_mTez_gEGlm-M",
    "art_QSosCVksWXEn",
    "art_kLtQwEBHGxMC",
    "art_xARDI4vSzSaY",
    "art_8QZZQJeOU5Rq",
    "art_YmPR0ovA6j-x",
    "art_Xdob_iGyaEzz",
    "art_k2gRJvCNxtot",
    "art_maps-Tw6ASn7",
    "art_Y0z08J69v1Gz",
    "art_VuYFuGdgNbjF",
    "art_g5RPpxg7Itqw",
    "art_gCleUgSr3wrU",
    "art__i9P9xJWIT6S",
    "art_obyUE2MdPQWZ",
    "art_ruL9_6y5xbrA",
    "art_TjlR8Ly_7t7P",
    "art_TaAMhDL3KbgM",
    "art_F4RRHsqnZH8U",
    "art_2XXh8xXc7nxg",
    "art_yQUePTDy_sfd",
    "art_LvKudy1yRCzj",
    "art_qJ6u7AFZAF-C",
    "art_XlJfiPLVzCTM",
    "art_SUH9xmX12sEv",
    "art_ufCkAm88vRZn",
    "art_8EPcaxpfeI06"
  ],
  "publishedAt": "2026-03-22T06:51:43.052Z",
  "updatedAt": "2026-03-24T18:25:04.931Z",
  "createdAt": "2026-03-22T06:51:40.361Z",
  "apiAccess": {
    "endpoints": {
      "search": "/api/v1/search?q=function-calling-best-practices-structured-output-and-tool-call-optimization",
      "json": "/api/v1/articles/function-calling-best-practices-structured-output-and-tool-call-optimization?format=json&lang=zh",
      "markdown": "/api/v1/articles/function-calling-best-practices-structured-output-and-tool-call-optimization?format=markdown&lang=zh"
    },
    "exampleUsage": "curl \"https://buzhou.io/api/v1/articles/function-calling-best-practices-structured-output-and-tool-call-optimization?format=json&lang=zh\""
  }
}