This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
検索結果コンテンツブロックは、適切なソース属性を持つ自然な引用を有効にし、Webサーチ品質の引用をカスタムアプリケーションにもたらします。この機能は、Claudeが正確にソースを引用する必要があるRAG(検索拡張生成)アプリケーションに特に強力です。
検索結果機能は、以下のモデルで利用可能です:
claude-opus-4-7)claude-opus-4-6)claude-sonnet-4-6)claude-sonnet-4-5-20250929)claude-opus-4-5-20251101)claude-opus-4-1-20250805)claude-opus-4-20250514)claude-sonnet-4-20250514)claude-3-7-sonnet-20250219)claude-haiku-4-5-20251001)claude-3-5-haiku-20241022)検索結果は2つの方法で提供できます:
どちらの場合でも、Claudeは適切なソース属性を持つ検索結果から情報を自動的に引用できます。
検索結果は以下の構造を使用します:
{
"type": "search_result",
"source": "https://example.com/article", // 必須:ソースURLまたは識別子
"title": "Article Title", // 必須:結果のタイトル
"content": [
// 必須:テキストブロックの配列
{
"type": "text",
"text": "The actual content of the search result..."
}
],
"citations": {
// オプション:引用設定
"enabled": true // この結果の引用を有効/無効にする
}
}| フィールド | 型 | 説明 |
|---|---|---|
type | string | "search_result"である必要があります |
source | string | コンテンツのソースURLまたは識別子 |
title | string | 検索結果の説明的なタイトル |
content | array | 実際のコンテンツを含むテキストブロックの配列 |
| フィールド | 型 | 説明 |
|---|---|---|
citations | object | enabledブール値フィールドを持つ引用設定 |
cache_control | object | キャッシュ制御設定(例:{"type": "ephemeral"}) |
content配列内の各項目は、以下を含むテキストブロックである必要があります:
type:"text"である必要がありますtext:実際のテキストコンテンツ(空でない文字列)最も強力なユースケースは、カスタムツールから検索結果を返すことです。これにより、ツールが関連コンテンツを取得して自動引用で返す動的RAGアプリケーションが有効になります。
from anthropic.types import (
MessageParam,
TextBlockParam,
SearchResultBlockParam,
ToolResultBlockParam,
)
client = Anthropic()
# ナレッジベース検索ツールを定義
knowledge_base_tool = {
"name": "search_knowledge_base",
"description": "Search the company knowledge base for information",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string", "description": "The search query"}},
"required": ["query"],
},
}
# ツール呼び出しを処理する関数
def search_knowledge_base(query):
# ここに検索ロジックを記述
# 正しい形式で検索結果を返す
return [
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/product-guide",
title="Product Configuration Guide",
content=[
TextBlockParam(
type="text",
text="To configure the product, navigate to Settings > Configuration. The default timeout is 30 seconds, but can be adjusted between 10-120 seconds based on your needs.",
)
],
citations={"enabled": True},
),
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/troubleshooting",
title="Troubleshooting Guide",
content=[
TextBlockParam(
type="text",
text="If you encounter timeout errors, first check the configuration settings. Common causes include network latency and incorrect timeout values.",
)
],
citations={"enabled": True},
),
]
# ツール付きメッセージを作成
response = client.messages.create(
model="claude-opus-4-7", # Works with all supported models
max_tokens=1024,
tools=[knowledge_base_tool],
messages=[
MessageParam(role="user", content="How do I configure the timeout settings?")
],
)
# Claudeがツールを呼び出すときに、検索結果を提供
if response.content[0].type == "tool_use":
tool_result = search_knowledge_base(response.content[0].input["query"])
# ツール結果を返す
final_response = client.messages.create(
model="claude-opus-4-7", # Works with all supported models
max_tokens=1024,
messages=[
MessageParam(
role="user", content="How do I configure the timeout settings?"
),
MessageParam(role="assistant", content=response.content),
MessageParam(
role="user",
content=[
ToolResultBlockParam(
type="tool_result",
tool_use_id=response.content[0].id,
content=tool_result, # Search results go here
)
],
),
],
)ユーザーメッセージで直接検索結果を提供することもできます。これは以下の場合に便利です:
from anthropic.types import MessageParam, TextBlockParam, SearchResultBlockParam
client = Anthropic()
# Provide search results directly in the user message
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
messages=[
MessageParam(
role="user",
content=[
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/api-reference",
title="API Reference - Authentication",
content=[
TextBlockParam(
type="text",
text="All API requests must include an API key in the Authorization header. Keys can be generated from the dashboard. Rate limits: 1000 requests per hour for standard tier, 10000 for premium.",
)
],
citations={"enabled": True},
),
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/quickstart",
title="Getting Started Guide",
content=[
TextBlockParam(
type="text",
text="To get started: 1) Sign up for an account, 2) Generate an API key from the dashboard, 3) Install our SDK using pip install company-sdk, 4) Initialize the client with your API key.",
)
],
citations={"enabled": True},
),
TextBlockParam(
type="text",
text="Based on these search results, how do I authenticate API requests and what are the rate limits?",
),
],
)
],
)
print(response.model_dump_json(indent=2))検索結果がどのように提供されるかに関わらず、Claudeはそれらから情報を使用する際に自動的に引用を含めます:
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "APIリクエストを認証するには、認可ヘッダーにAPIキーを含める必要があります",
"citations": [
{
"type": "search_result_location",
"source": "https://docs.company.com/api-reference",
"title": "API Reference - Authentication",
"cited_text": "All API requests must include an API key in the Authorization header",
"search_result_index": 0,
"start_block_index": 0,
"end_block_index": 0
}
]
},
{
"type": "text",
"text": "。APIキーはダッシュボードから生成できます",
"citations": [
{
"type": "search_result_location",
"source": "https://docs.company.com/api-reference",
"title": "API Reference - Authentication",
"cited_text": "Keys can be generated from the dashboard",
"search_result_index": 0,
"start_block_index": 0,
"end_block_index": 0
}
]
},
{
"type": "text",
"text": "。レート制限は、スタンダードティアで1時間あたり1,000リクエスト、プレミアムティアで1時間あたり10,000リクエストです。",
"citations": [
{
"type": "search_result_location",
"source": "https://docs.company.com/api-reference",
"title": "API Reference - Authentication",
"cited_text": "Rate limits: 1000 requests per hour for standard tier, 10000 for premium",
"search_result_index": 0,
"start_block_index": 0,
"end_block_index": 0
}
]
}
]
}各引用には以下が含まれます:
| フィールド | タイプ | 説明 |
|---|---|---|
type | 文字列 | 検索結果の引用では常に"search_result_location" |
source | 文字列 | 元の検索結果からのソース |
title | 文字列またはnull | 元の検索結果からのタイトル |
cited_text | 文字列 | 引用されている正確なテキスト |
search_result_index | 整数 | 検索結果のインデックス(0ベース) |
start_block_index | 整数 | コンテンツ配列内の開始位置 |
end_block_index | 整数 | コンテンツ配列内の終了位置 |
注: search_result_indexは、検索結果がどのように提供されたか(ツール呼び出しまたはトップレベルコンテンツ)に関わらず、検索結果コンテンツブロックのインデックス(0ベース)を指します。
検索結果はcontent配列に複数のテキストブロックを含めることができます:
{
"type": "search_result",
"source": "https://docs.company.com/api-guide",
"title": "API Documentation",
"content": [
{
"type": "text",
"text": "Authentication: All API requests require an API key."
},
{
"type": "text",
"text": "Rate Limits: The API allows 1000 requests per hour per key."
},
{
"type": "text",
"text": "Error Handling: The API returns standard HTTP status codes."
}
]
}Claudeはstart_block_indexとend_block_indexフィールドを使用して特定のブロックを引用できます。
同じ会話でツールベースとトップレベルの検索結果の両方を使用できます:
from anthropic.types import MessageParam, SearchResultBlockParam, TextBlockParam
# トップレベル検索結果を含む最初のメッセージ
messages = [
MessageParam(
role="user",
content=[
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/overview",
title="Product Overview",
content=[
TextBlockParam(
type="text", text="Our product helps teams collaborate..."
)
],
citations={"enabled": True},
),
TextBlockParam(
type="text",
text="Tell me about this product and search for pricing information",
),
],
)
]
# Claudeが応答し、価格情報を検索するためにツールを呼び出す可能性があります
# その後、より多くの検索結果を含むツール結果を提供します両方の方法は、検索結果を他のコンテンツと混合することをサポートしています:
from anthropic.types import SearchResultBlockParam, TextBlockParam
# ツール結果内
tool_result = [
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/guide",
title="User Guide",
content=[TextBlockParam(type="text", text="Configuration details...")],
citations={"enabled": True},
),
TextBlockParam(
type="text", text="Additional context: This applies to version 2.0 and later."
),
]
# トップレベルコンテンツ内
user_content = [
SearchResultBlockParam(
type="search_result",
source="https://research.com/paper",
title="Research Paper",
content=[TextBlockParam(type="text", text="Key findings...")],
citations={"enabled": True},
),
{
"type": "image",
"source": {"type": "url", "url": "https://example.com/chart.png"},
},
TextBlockParam(
type="text", text="How does the chart relate to the research findings?"
),
]より良いパフォーマンスのためにキャッシュ制御を追加します:
{
"type": "search_result",
"source": "https://docs.company.com/guide",
"title": "User Guide",
"content": [{ "type": "text", "text": "..." }],
"cache_control": {
"type": "ephemeral"
}
}デフォルトでは、検索結果の引用は無効になっています。citations設定を明示的に設定することで引用を有効にできます:
{
"type": "search_result",
"source": "https://docs.company.com/guide",
"title": "User Guide",
"content": [{ "type": "text", "text": "Important documentation..." }],
"citations": {
"enabled": true // この結果の引用を有効にします
}
}citations.enabledがtrueに設定されている場合、Claudeは検索結果から情報を使用する際に引用参照を含めます。これにより以下が可能になります:
citationsフィールドが省略されている場合、デフォルトで引用は無効になります。
引用はすべてか無かです: リクエスト内のすべての検索結果で引用を有効にするか、すべてで無効にするかのいずれかです。異なる引用設定を持つ検索結果を混合するとエラーが発生します。一部のソースの引用を無効にする必要がある場合は、そのリクエスト内のすべての検索結果で引用を無効にする必要があります。
結果を効果的に構造化する
一貫性を保つ
エラーを適切に処理する
def search_with_fallback(query):
try:
results = perform_search(query)
if not results:
return {"type": "text", "text": "No results found."}
return format_as_search_results(results)
except Exception as e:
return {"type": "text", "text": f"Search error: {str(e)}"}content配列には少なくとも1つのテキストブロックが含まれている必要がありますWas this page helpful?