-
Notifications
You must be signed in to change notification settings - Fork 314
feat: claude citation support with BedrockModel #631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theagenticguy
wants to merge
17
commits into
strands-agents:main
Choose a base branch
from
theagenticguy:claude-citations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c586720
feat: add citations to document content
theagenticguy a156321
feat: addes citation types
theagenticguy 20ee330
chore: remove uv.lock
theagenticguy 88ebbeb
test: add letter.pdf for test-integ
theagenticguy 7f36078
feat: working bedrock citations feature
theagenticguy 4d55809
Merge branch 'main' into claude-citations
theagenticguy 441e583
feat: fail early for citations with incompatible models
theagenticguy 08b8bdb
fix: validates model ids with cross region inference ids
theagenticguy df2e579
Merge branch 'main' into claude-citations
theagenticguy 7e70bf7
Apply suggestion from @Unshure
theagenticguy 8bbd8b8
fix: addresses comments
theagenticguy 3cdad78
Merge branch 'main' into claude-citations
theagenticguy 3a2bc9f
Merge branch 'strands-agents:main' into claude-citations
theagenticguy f4bc306
fix: tests failing
theagenticguy b9a5f1f
Update src/strands/models/bedrock.py
theagenticguy a4d825d
Update src/strands/models/bedrock.py
theagenticguy cf701d9
Merge branch 'strands-agents:main' into claude-citations
theagenticguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import json | ||
import logging | ||
import os | ||
from typing import Any, AsyncGenerator, Callable, Iterable, Literal, Optional, Type, TypeVar, Union | ||
from typing import Any, AsyncGenerator, Callable, Iterable, Literal, Optional, Type, TypeVar, Union, cast | ||
|
||
import boto3 | ||
from botocore.config import Config as BotocoreConfig | ||
|
@@ -18,7 +18,10 @@ | |
from ..event_loop import streaming | ||
from ..tools import convert_pydantic_to_tool_spec | ||
from ..types.content import ContentBlock, Message, Messages | ||
from ..types.exceptions import ContextWindowOverflowException, ModelThrottledException | ||
from ..types.exceptions import ( | ||
ContextWindowOverflowException, | ||
ModelThrottledException, | ||
) | ||
from ..types.streaming import StreamEvent | ||
from ..types.tools import ToolResult, ToolSpec | ||
from .model import Model | ||
|
@@ -510,7 +513,7 @@ def _convert_non_streaming_to_streaming(self, response: dict[str, Any]) -> Itera | |
yield {"messageStart": {"role": response["output"]["message"]["role"]}} | ||
|
||
# Process content blocks | ||
for content in response["output"]["message"]["content"]: | ||
for content in cast(list[ContentBlock], response["output"]["message"]["content"]): | ||
# Yield contentBlockStart event if needed | ||
if "toolUse" in content: | ||
yield { | ||
|
@@ -553,6 +556,25 @@ def _convert_non_streaming_to_streaming(self, response: dict[str, Any]) -> Itera | |
} | ||
} | ||
} | ||
elif "citationsContent" in content: | ||
# For non-streaming citations, emit text and metadata deltas in sequence | ||
# to match streaming behavior where they flow naturally | ||
if "content" in content["citationsContent"]: | ||
text_content = "".join([content["text"] for content in content["citationsContent"]["content"]]) | ||
yield { | ||
"contentBlockDelta": {"delta": {"text": text_content}}, | ||
} | ||
|
||
for citation in content["citationsContent"]["citations"]: | ||
# Then emit citation metadata (for structure) | ||
from ..types.streaming import CitationsDelta | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets be consistent and just import this at the top |
||
|
||
citation_metadata: CitationsDelta = { | ||
"title": citation["title"], | ||
"location": citation["location"], | ||
"sourceContent": citation["sourceContent"], | ||
} | ||
yield {"contentBlockDelta": {"delta": {"citation": citation_metadata}}} | ||
|
||
# Yield contentBlockStop event | ||
yield {"contentBlockStop": {}} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
"""Citation type definitions for the SDK. | ||
|
||
These types are modeled after the Bedrock API. | ||
""" | ||
|
||
from typing import List, Union | ||
|
||
from typing_extensions import TypedDict | ||
|
||
|
||
class CitationsConfig(TypedDict): | ||
"""Configuration for enabling citations on documents. | ||
|
||
Attributes: | ||
enabled: Whether citations are enabled for this document. | ||
""" | ||
|
||
enabled: bool | ||
|
||
|
||
class DocumentCharLocation(TypedDict, total=False): | ||
"""Specifies a character-level location within a document. | ||
|
||
Provides precise positioning information for cited content using | ||
start and end character indices. | ||
|
||
Attributes: | ||
documentIndex: The index of the document within the array of documents | ||
provided in the request. Minimum value of 0. | ||
start: The starting character position of the cited content within | ||
the document. Minimum value of 0. | ||
end: The ending character position of the cited content within | ||
the document. Minimum value of 0. | ||
""" | ||
|
||
documentIndex: int | ||
start: int | ||
end: int | ||
|
||
|
||
class DocumentChunkLocation(TypedDict, total=False): | ||
"""Specifies a chunk-level location within a document. | ||
|
||
Provides positioning information for cited content using logical | ||
document segments or chunks. | ||
|
||
Attributes: | ||
documentIndex: The index of the document within the array of documents | ||
provided in the request. Minimum value of 0. | ||
start: The starting chunk identifier or index of the cited content | ||
within the document. Minimum value of 0. | ||
end: The ending chunk identifier or index of the cited content | ||
within the document. Minimum value of 0. | ||
""" | ||
|
||
documentIndex: int | ||
start: int | ||
end: int | ||
|
||
|
||
class DocumentPageLocation(TypedDict, total=False): | ||
"""Specifies a page-level location within a document. | ||
|
||
Provides positioning information for cited content using page numbers. | ||
|
||
Attributes: | ||
documentIndex: The index of the document within the array of documents | ||
provided in the request. Minimum value of 0. | ||
start: The starting page number of the cited content within | ||
the document. Minimum value of 0. | ||
end: The ending page number of the cited content within | ||
the document. Minimum value of 0. | ||
""" | ||
|
||
documentIndex: int | ||
start: int | ||
end: int | ||
|
||
|
||
# Union type for citation locations | ||
CitationLocation = Union[DocumentCharLocation, DocumentChunkLocation, DocumentPageLocation] | ||
|
||
|
||
class CitationSourceContent(TypedDict, total=False): | ||
"""Contains the actual text content from a source document. | ||
|
||
Contains the actual text content from a source document that is being | ||
cited or referenced in the model's response. | ||
|
||
Note: | ||
This is a UNION type, so only one of the members can be specified. | ||
|
||
Attributes: | ||
text: The text content from the source document that is being cited. | ||
""" | ||
|
||
text: str | ||
|
||
|
||
class CitationGeneratedContent(TypedDict, total=False): | ||
"""Contains the generated text content that corresponds to a citation. | ||
|
||
Contains the generated text content that corresponds to or is supported | ||
by a citation from a source document. | ||
|
||
Note: | ||
This is a UNION type, so only one of the members can be specified. | ||
|
||
Attributes: | ||
text: The text content that was generated by the model and is | ||
supported by the associated citation. | ||
""" | ||
|
||
text: str | ||
|
||
|
||
class Citation(TypedDict, total=False): | ||
"""Contains information about a citation that references a source document. | ||
|
||
Citations provide traceability between the model's generated response | ||
and the source documents that informed that response. | ||
|
||
Attributes: | ||
location: The precise location within the source document where the | ||
cited content can be found, including character positions, page | ||
numbers, or chunk identifiers. | ||
sourceContent: The specific content from the source document that was | ||
referenced or cited in the generated response. | ||
title: The title or identifier of the source document being cited. | ||
""" | ||
|
||
location: CitationLocation | ||
sourceContent: List[CitationSourceContent] | ||
title: str | ||
|
||
|
||
class CitationsContentBlock(TypedDict, total=False): | ||
"""A content block containing generated text and associated citations. | ||
|
||
This block type is returned when document citations are enabled, providing | ||
traceability between the generated content and the source documents that | ||
informed the response. | ||
|
||
Attributes: | ||
citations: An array of citations that reference the source documents | ||
used to generate the associated content. | ||
content: The generated content that is supported by the associated | ||
citations. | ||
""" | ||
|
||
citations: List[Citation] | ||
content: List[CitationGeneratedContent] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.