Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pydantic_ai_slim/pydantic_ai/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
PromptVariableValuesTypeDef,
ReasoningContentBlockOutputTypeDef,
S3LocationTypeDef,
ServiceTierTypeDef,
SystemContentBlockTypeDef,
ToolChoiceTypeDef,
ToolConfigurationTypeDef,
Expand Down Expand Up @@ -231,6 +232,12 @@ class BedrockModelSettings(ModelSettings, total=False):
See https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html for more information.
"""

bedrock_service_tier: ServiceTierTypeDef
"""Setting for optimizing performance and cost

See more about it on <https://docs.aws.amazon.com/bedrock/latest/userguide/service-tiers-inference.html>.
"""


@dataclass(init=False)
class BedrockConverseModel(Model):
Expand Down Expand Up @@ -481,6 +488,8 @@ async def _messages_create(
params['additionalModelRequestFields'] = additional_model_requests_fields
if prompt_variables := model_settings.get('bedrock_prompt_variables', None):
params['promptVariables'] = prompt_variables
if service_tier := model_settings.get('bedrock_service_tier', None):
params['serviceTier'] = service_tier

try:
if stream:
Expand Down
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ anthropic = ["anthropic>=0.75.0"]
groq = ["groq>=0.25.0"]
openrouter = ["openai>=2.8.0"]
mistral = ["mistralai>=1.9.10"]
bedrock = ["boto3>=1.40.14"]
bedrock = ["boto3>=1.42.14"]
huggingface = ["huggingface-hub[inference]>=0.33.5,<1.0.0"]
outlines-transformers = ["outlines[transformers]>=1.0.0, <1.3.0; (sys_platform != 'darwin' or platform_machine != 'x86_64')", "transformers>=4.0.0", "pillow", "torch; (sys_platform != 'darwin' or platform_machine != 'x86_64')"]
outlines-llamacpp = ["outlines[llamacpp]>=1.0.0, <1.3.0"]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ dev = [
"pytest-pretty>=1.3.0",
"pytest-recording>=0.13.2",
"diff-cover>=9.2.0",
"boto3-stubs[bedrock-runtime]",
"boto3-stubs[bedrock-runtime]>=1.42.13",
"strict-no-cover @ git+https://github.com/pydantic/strict-no-cover.git@7fc59da2c4dff919db2095a0f0e47101b657131d",
"pytest-xdist>=3.6.1",
# Needed for PyCharm users
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
interactions:
- request:
body: '{"messages": [{"role": "user", "content": [{"text": "What is the capital of France?"}]}], "system": [{"text": "You
are a helpful chatbot."}], "serviceTier": {"type": "default"}}'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cassette request mismatches test code service tier value

The test specifies bedrock_service_tier={'type': 'flex'} but the cassette file shows "serviceTier": {"type": "default"} in the recorded request body. This mismatch means the test isn't validating what it claims to test. The cassette appears to have been recorded with a different value than what the test code now uses, making the test unreliable for catching regressions in the bedrock_service_tier functionality.

Additional Locations (1)

Fix in Cursor Fix in Web

headers:
amz-sdk-invocation-id:
- !!binary |
ZmZhYjMyZmItODRjOS00YWZjLWE4NTAtNTQ4OTUxMjI5NmU4
amz-sdk-request:
- !!binary |
YXR0ZW1wdD0x
content-length:
- '178'
content-type:
- !!binary |
YXBwbGljYXRpb24vanNvbg==
method: POST
uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/us.amazon.nova-micro-v1%3A0/converse
response:
headers:
connection:
- keep-alive
content-length:
- '206'
content-type:
- application/json
parsed_body:
metrics:
latencyMs: 159
output:
message:
content:
- text: The capital of France is Paris. Paris is not only the capital city but also the most populous city in France, known for its significant cultural,
political, and economic influence both within the country and globally. It is famous for landmarks such as the Eiffel Tower, the Louvre Museum,
and the Notre-Dame Cathedral, among many other historical and architectural treasures.

role: assistant
stopReason: max_tokens
usage:
inputTokens: 13
outputTokens: 5
totalTokens: 18
serviceTier:
type: flex
status:
code: 200
message: OK
version: 1
10 changes: 10 additions & 0 deletions tests/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ async def test_bedrock_model_other_parameters(allow_model_requests: None, bedroc
)


async def test_bedrock_model_service_tier(allow_model_requests: None, bedrock_provider: BedrockProvider):
model = BedrockConverseModel('us.amazon.nova-micro-v1:0', provider=bedrock_provider)
model_settings = BedrockModelSettings(bedrock_service_tier={'type': 'flex'})
agent = Agent(model=model, system_prompt='You are a helpful chatbot.', model_settings=model_settings)
result = await agent.run('What is the capital of France?')
assert result.output == snapshot(
'The capital of France is Paris. Paris is not only the capital city but also the most populous city in France, known for its significant cultural, political, and economic influence both within the country and globally. It is famous for landmarks such as the Eiffel Tower, the Louvre Museum, and the Notre-Dame Cathedral, among many other historical and architectural treasures.'
)


async def test_bedrock_model_iter_stream(allow_model_requests: None, bedrock_provider: BedrockProvider):
model = BedrockConverseModel('us.amazon.nova-micro-v1:0', provider=bedrock_provider)
agent = Agent(model=model, system_prompt='You are a helpful chatbot.', model_settings={'top_p': 0.5})
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.