Skip to content

Commit b94ee19

Browse files
arde100Kludex
andauthored
fix: allow multiple BinaryContent in message_history (#1523)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
1 parent bbea4d5 commit b94ee19

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

pydantic_ai_slim/pydantic_ai/models/bedrock.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import functools
44
import typing
5-
from collections.abc import AsyncIterator, Iterable, Mapping
5+
from collections.abc import AsyncIterator, Iterable, Iterator, Mapping
66
from contextlib import asynccontextmanager
77
from dataclasses import dataclass, field
88
from datetime import datetime
9+
from itertools import count
910
from typing import TYPE_CHECKING, Any, Generic, Literal, Union, cast, overload
1011

1112
import anyio
@@ -369,13 +370,14 @@ async def _map_messages(
369370
"""Just maps a `pydantic_ai.Message` to the Bedrock `MessageUnionTypeDef`."""
370371
system_prompt: list[SystemContentBlockTypeDef] = []
371372
bedrock_messages: list[MessageUnionTypeDef] = []
373+
document_count: Iterator[int] = count(1)
372374
for m in messages:
373375
if isinstance(m, ModelRequest):
374376
for part in m.parts:
375377
if isinstance(part, SystemPromptPart):
376378
system_prompt.append({'text': part.content})
377379
elif isinstance(part, UserPromptPart):
378-
bedrock_messages.extend(await self._map_user_prompt(part))
380+
bedrock_messages.extend(await self._map_user_prompt(part, document_count))
379381
elif isinstance(part, ToolReturnPart):
380382
assert part.tool_call_id is not None
381383
bedrock_messages.append(
@@ -430,20 +432,18 @@ async def _map_messages(
430432
return system_prompt, bedrock_messages
431433

432434
@staticmethod
433-
async def _map_user_prompt(part: UserPromptPart) -> list[MessageUnionTypeDef]:
435+
async def _map_user_prompt(part: UserPromptPart, document_count: Iterator[int]) -> list[MessageUnionTypeDef]:
434436
content: list[ContentBlockUnionTypeDef] = []
435437
if isinstance(part.content, str):
436438
content.append({'text': part.content})
437439
else:
438-
document_count = 0
439440
for item in part.content:
440441
if isinstance(item, str):
441442
content.append({'text': item})
442443
elif isinstance(item, BinaryContent):
443444
format = item.format
444445
if item.is_document:
445-
document_count += 1
446-
name = f'Document {document_count}'
446+
name = f'Document {next(document_count)}'
447447
assert format in ('pdf', 'txt', 'csv', 'doc', 'docx', 'xls', 'xlsx', 'html', 'md')
448448
content.append({'document': {'name': name, 'format': format, 'source': {'bytes': item.data}}})
449449
elif item.is_image:
@@ -464,8 +464,7 @@ async def _map_user_prompt(part: UserPromptPart) -> list[MessageUnionTypeDef]:
464464
content.append({'image': image})
465465

466466
elif item.kind == 'document-url':
467-
document_count += 1
468-
name = f'Document {document_count}'
467+
name = f'Document {next(document_count)}'
469468
data = response.content
470469
content.append({'document': {'name': name, 'format': item.format, 'source': {'bytes': data}}})
471470

0 commit comments

Comments
 (0)