Skip to content

Commit 13d7433

Browse files
committed
Add support on Groq
1 parent f33e568 commit 13d7433

File tree

5 files changed

+123
-9
lines changed

5 files changed

+123
-9
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ repos:
6060
rev: v2.3.0
6161
hooks:
6262
- id: codespell
63-
args: ['--skip', 'tests/models/cassettes/*,docs/a2a/fasta2a.md']
63+
args: ['--skip', 'tests/models/cassettes/*,docs/a2a/fasta2a.md,tests/models/test_groq.py']
6464
additional_dependencies:
6565
- tomli

pydantic_ai_slim/pydantic_ai/models/groq.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing_extensions import assert_never
1111

1212
from .. import ModelHTTPError, UnexpectedModelBehavior, _utils, usage
13-
from .._utils import guard_tool_call_id as _guard_tool_call_id
13+
from .._utils import generate_tool_call_id, guard_tool_call_id as _guard_tool_call_id
1414
from ..messages import (
1515
BinaryContent,
1616
DocumentUrl,
@@ -21,6 +21,8 @@
2121
ModelResponsePart,
2222
ModelResponseStreamEvent,
2323
RetryPromptPart,
24+
ServerToolCallPart,
25+
ServerToolReturnPart,
2426
SystemPromptPart,
2527
TextPart,
2628
ToolCallPart,
@@ -205,7 +207,7 @@ async def _completions_create(
205207
extra_headers = model_settings.get('extra_headers', {})
206208
extra_headers.setdefault('User-Agent', get_user_agent())
207209
return await self.client.chat.completions.create(
208-
model=str(self._model_name),
210+
model=self._model_name,
209211
messages=groq_messages,
210212
n=1,
211213
parallel_tool_calls=model_settings.get('parallel_tool_calls', NOT_GIVEN),
@@ -234,7 +236,15 @@ def _process_response(self, response: chat.ChatCompletion) -> ModelResponse:
234236
timestamp = datetime.fromtimestamp(response.created, tz=timezone.utc)
235237
choice = response.choices[0]
236238
items: list[ModelResponsePart] = []
237-
# TODO(Marcelo): The `choice.message.executed_tools` has the server tools executed by Groq.
239+
if choice.message.executed_tools:
240+
for tool in choice.message.executed_tools:
241+
tool_call_id = generate_tool_call_id()
242+
items.append(
243+
ServerToolCallPart(
244+
tool_name=tool.type, args=tool.arguments, model_name='groq', tool_call_id=tool_call_id
245+
)
246+
)
247+
items.append(ServerToolReturnPart(tool_name=tool.type, content=tool.output, tool_call_id=tool_call_id))
238248
if choice.message.content is not None:
239249
items.append(TextPart(content=choice.message.content))
240250
if choice.message.tool_calls is not None:

pydantic_ai_slim/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ cohere = ["cohere>=5.13.11; platform_system != 'Emscripten'"]
6666
vertexai = ["google-auth>=2.36.0", "requests>=2.32.2"]
6767
google = ["google-genai>=1.15.0"]
6868
anthropic = ["anthropic>=0.52.0"]
69-
groq = ["groq>=0.15.0"]
69+
groq = ["groq>=0.25.0"]
7070
mistral = ["mistralai>=1.2.5"]
7171
bedrock = ["boto3>=1.35.74"]
7272
# Tools

tests/models/test_groq.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
ModelRequest,
2323
ModelResponse,
2424
RetryPromptPart,
25+
ServerToolCallPart,
26+
ServerToolReturnPart,
2527
SystemPromptPart,
2628
TextPart,
2729
ToolCallPart,
@@ -692,3 +694,105 @@ async def test_groq_model_web_search_tool(allow_model_requests: None, groq_api_k
692694

693695
result = await agent.run('What day is today?')
694696
assert result.output == snapshot('The current day is Tuesday.')
697+
assert result.all_messages() == snapshot(
698+
[
699+
ModelRequest(parts=[UserPromptPart(content='What day is today?', timestamp=IsDatetime())]),
700+
ModelResponse(
701+
parts=[
702+
ServerToolCallPart(
703+
tool_name='search',
704+
args='{"query": "What is the current date?"}',
705+
tool_call_id='0',
706+
model_name='groq',
707+
),
708+
ServerToolReturnPart(
709+
tool_name='search',
710+
content="""\
711+
Title: Today's Date - Find Out Quickly What's The Date Today ️
712+
URL: https://calendarhours.com/todays-date/
713+
Content: The current date in RFC 2822 Format with shortened day of week, numerical date, three-letter month abbreviation, year, time, and time zone is: Tue, 13 May 2025 06:07:56 -0400; The current date in Unix Epoch Format with number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT) is:
714+
Score: 0.8299
715+
716+
Title: Today's Date | Current date now - MaxTables
717+
URL: https://maxtables.com/tools/todays-date.html
718+
Content: The current date, including day of the week, month, day, and year. The exact time, down to seconds. Details on the time zone, its location, and its GMT difference. A tool to select the present date. A visual calendar chart. Why would I need to check Today's Date on this platform instead of my device?
719+
Score: 0.7223
720+
721+
Title: Current Time and Date - Exact Time!
722+
URL: https://time-and-calendar.com/
723+
Content: The actual time is: Mon May 12 2025 22:14:39 GMT-0700 (Pacific Daylight Time) Your computer time is: 22:14:38 The time of your computer is synchronized with our web server. This mean that it is synchonizing in real time with our server clock.
724+
Score: 0.6799
725+
726+
Title: Today's Date - CalendarDate.com
727+
URL: https://www.calendardate.com/todays.htm
728+
Content: Details about today's date with count of days, weeks, and months, Sun and Moon cycles, Zodiac signs and holidays. Monday May 12, 2025 . Home; Calendars. 2025 Calendar; ... Current Season Today: Spring with 40 days until the start of Summer. S. Hemishpere flip seasons - i.e. Winter is Summer.
729+
Score: 0.6416
730+
731+
Title: What is the date today | Today's Date
732+
URL: https://www.datetoday.info/
733+
Content: Master time tracking with Today's Date. Stay updated with real-time information on current date, time, day of the week, days left in the week, current day and remaining days of the year. Explore time in globally accepted formats. Keep up with the current week and month, along with the remaining weeks and months for the year. Embrace efficient time tracking with Today's Date.
734+
Score: 0.6282
735+
736+
Title: Explore Today's Date, Time Zones, Holidays & More
737+
URL: https://whatdateis.today/
738+
Content: Check what date and time it is today (May 8, 2025). View current time across different time zones, upcoming holidays, and use our date calculator. Your one-stop destination for all date and time information.
739+
Score: 0.6181
740+
741+
Title: Today's Date and Time - Date and Time Tools
742+
URL: https://todaysdatetime.com/
743+
Content: Discover today's exact date and time, learn about time zones, date formats, and explore our comprehensive collection of date and time tools including calculators, converters, and calendars. ... Get the exact current date and time, along with powerful calculation tools for all your scheduling needs. 12h. Today. Day 76 of year (366) Yesterday
744+
Score: 0.5456
745+
746+
Title: Current Time Now - What time is it? - RapidTables.com
747+
URL: https://www.rapidtables.com/tools/current-time.html
748+
Content: This page includes the following information: Current time: hours, minutes, seconds. Today's date: day of week, month, day, year. Time zone with location and GMT offset.
749+
Score: 0.4255
750+
751+
Title: Current Time
752+
URL: https://www.timeanddate.com/
753+
Content: Welcome to the world's top site for time, time zones, and astronomy. Organize your life with free online info and tools you can rely on. No sign-up needed. Sign in. News. News Home; Astronomy News; ... Current Time. Monday May 12, 2025 Roanoke Rapids, North Carolina, USA. Set home location. 11:27: 03 pm. World Clock.
754+
Score: 0.3876
755+
756+
Title: Current local time in the United States - World clock
757+
URL: https://dateandtime.info/country.php?code=US
758+
Content: Time and Date of DST Change Time Change; DST started: Sunday, March 9, 2025 at 2:00 AM: The clocks were put forward an hour to 3:00 AM. DST ends: Sunday, November 2, 2025 at 2:00 AM: The clocks will be put back an hour to 1:00 AM. DST starts: Sunday, March 8, 2026 at 2:00 AM: The clocks will be put forward an hour to 3:00 AM.
759+
Score: 0.3042
760+
761+
Title: Time.is - exact time, any time zone
762+
URL: https://time.is/
763+
Content: 7 million locations, 58 languages, synchronized with atomic clock time. Time.is. Get Time.is Ad-free! Exact time now: 05:08:45. Tuesday, 13 May, 2025, week 20. Sun: ↑ 05:09 ↓ 20:45 (15h 36m) - More info - Make London time default - Remove from favorite locations
764+
Score: 0.2796
765+
766+
Title: Time in United States now
767+
URL: https://time.is/United_States
768+
Content: Exact time now, time zone, time difference, sunrise/sunset time and key facts for United States. Time.is. Get Time.is Ad-free! Time in United States now . 11:17:42 PM. Monday, May 12, 2025. United States (incl. dependent territories) has 11 time zones. The time zone for the capital Washington, D.C. is used here.
769+
Score: 0.2726
770+
771+
Title: Current Local Time in the United States - timeanddate.com
772+
URL: https://www.timeanddate.com/worldclock/usa
773+
Content: United States time now. USA time zones and time zone map with current time in each state.
774+
Score: 0.2519
775+
776+
Title: Current local time in United States - World Time Clock & Map
777+
URL: https://24timezones.com/United-States/time
778+
Content: Check the current time in United States and time zone information, the UTC offset and daylight saving time dates in 2025.
779+
Score: 0.2221
780+
781+
Title: The World Clock — Worldwide - timeanddate.com
782+
URL: https://www.timeanddate.com/worldclock/
783+
Content: World time and date for cities in all time zones. International time right now. Takes into account all DST clock changes.
784+
Score: 0.2134
785+
786+
""",
787+
tool_call_id='0',
788+
timestamp=IsDatetime(),
789+
),
790+
TextPart(content='The current day is Tuesday.'),
791+
],
792+
usage=Usage(requests=1, request_tokens=4287, response_tokens=117, total_tokens=4404),
793+
model_name='compound-beta',
794+
timestamp=IsDatetime(),
795+
vendor_id='stub',
796+
),
797+
]
798+
)

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)