-
Notifications
You must be signed in to change notification settings - Fork 794
/
Copy pathgemini.py
858 lines (691 loc) · 34.4 KB
/
gemini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
from __future__ import annotations as _annotations
import base64
import warnings
from collections.abc import AsyncIterator, Sequence
from contextlib import asynccontextmanager
from dataclasses import dataclass, field, replace
from datetime import datetime
from typing import Annotated, Any, Literal, Protocol, Union, cast
from uuid import uuid4
import httpx
import pydantic
from httpx import USE_CLIENT_DEFAULT, Response as HTTPResponse
from typing_extensions import NotRequired, TypedDict, assert_never
from pydantic_ai.providers import Provider, infer_provider
from .. import ModelHTTPError, UnexpectedModelBehavior, UserError, _utils, usage
from ..messages import (
AudioUrl,
BinaryContent,
DocumentUrl,
ImageUrl,
ModelMessage,
ModelRequest,
ModelResponse,
ModelResponsePart,
ModelResponseStreamEvent,
RetryPromptPart,
SystemPromptPart,
TextPart,
ToolCallPart,
ToolReturnPart,
UserPromptPart,
VideoUrl,
)
from ..settings import ModelSettings
from ..tools import ToolDefinition
from . import (
Model,
ModelRequestParameters,
StreamedResponse,
cached_async_http_client,
check_allow_model_requests,
get_user_agent,
)
from ._json_schema import JsonSchema, WalkJsonSchema
LatestGeminiModelNames = Literal[
'gemini-1.5-flash',
'gemini-1.5-flash-8b',
'gemini-1.5-pro',
'gemini-1.0-pro',
'gemini-2.0-flash-exp',
'gemini-2.0-flash-thinking-exp-01-21',
'gemini-exp-1206',
'gemini-2.0-flash',
'gemini-2.0-flash-lite-preview-02-05',
'gemini-2.0-pro-exp-02-05',
'gemini-2.5-flash-preview-04-17',
'gemini-2.5-pro-exp-03-25',
'gemini-2.5-pro-preview-03-25',
]
"""Latest Gemini models."""
GeminiModelName = Union[str, LatestGeminiModelNames]
"""Possible Gemini model names.
Since Gemini supports a variety of date-stamped models, we explicitly list the latest models but
allow any name in the type hints.
See [the Gemini API docs](https://ai.google.dev/gemini-api/docs/models/gemini#model-variations) for a full list.
"""
class GeminiModelSettings(ModelSettings):
"""Settings used for a Gemini model request.
ALL FIELDS MUST BE `gemini_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.
"""
gemini_safety_settings: list[GeminiSafetySettings]
@dataclass(init=False)
class GeminiModel(Model):
"""A model that uses Gemini via `generativelanguage.googleapis.com` API.
This is implemented from scratch rather than using a dedicated SDK, good API documentation is
available [here](https://ai.google.dev/api).
Apart from `__init__`, all methods are private or match those of the base class.
"""
client: httpx.AsyncClient = field(repr=False)
_model_name: GeminiModelName = field(repr=False)
_provider: Literal['google-gla', 'google-vertex'] | Provider[httpx.AsyncClient] | None = field(repr=False)
_auth: AuthProtocol | None = field(repr=False)
_url: str | None = field(repr=False)
_system: str = field(default='gemini', repr=False)
def __init__(
self,
model_name: GeminiModelName,
*,
provider: Literal['google-gla', 'google-vertex'] | Provider[httpx.AsyncClient] = 'google-gla',
):
"""Initialize a Gemini model.
Args:
model_name: The name of the model to use.
provider: The provider to use for authentication and API access. Can be either the string
'google-gla' or 'google-vertex' or an instance of `Provider[httpx.AsyncClient]`.
If not provided, a new provider will be created using the other parameters.
"""
self._model_name = model_name
self._provider = provider
if isinstance(provider, str):
provider = infer_provider(provider)
self._system = provider.name
self.client = provider.client
self._url = str(self.client.base_url)
@property
def base_url(self) -> str:
assert self._url is not None, 'URL not initialized'
return self._url
async def request(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> tuple[ModelResponse, usage.Usage]:
check_allow_model_requests()
async with self._make_request(
messages, False, cast(GeminiModelSettings, model_settings or {}), model_request_parameters
) as http_response:
data = await http_response.aread()
response = _gemini_response_ta.validate_json(data)
return self._process_response(response), _metadata_as_usage(response)
@asynccontextmanager
async def request_stream(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> AsyncIterator[StreamedResponse]:
check_allow_model_requests()
async with self._make_request(
messages, True, cast(GeminiModelSettings, model_settings or {}), model_request_parameters
) as http_response:
yield await self._process_streamed_response(http_response)
def customize_request_parameters(self, model_request_parameters: ModelRequestParameters) -> ModelRequestParameters:
def _customize_tool_def(t: ToolDefinition):
return replace(t, parameters_json_schema=_GeminiJsonSchema(t.parameters_json_schema).walk())
return ModelRequestParameters(
function_tools=[_customize_tool_def(tool) for tool in model_request_parameters.function_tools],
allow_text_output=model_request_parameters.allow_text_output,
output_tools=[_customize_tool_def(tool) for tool in model_request_parameters.output_tools],
)
@property
def model_name(self) -> GeminiModelName:
"""The model name."""
return self._model_name
@property
def system(self) -> str:
"""The system / model provider."""
return self._system
def _get_tools(self, model_request_parameters: ModelRequestParameters) -> _GeminiTools | None:
tools = [_function_from_abstract_tool(t) for t in model_request_parameters.function_tools]
if model_request_parameters.output_tools:
tools += [_function_from_abstract_tool(t) for t in model_request_parameters.output_tools]
return _GeminiTools(function_declarations=tools) if tools else None
def _get_tool_config(
self, model_request_parameters: ModelRequestParameters, tools: _GeminiTools | None
) -> _GeminiToolConfig | None:
if model_request_parameters.allow_text_output:
return None
elif tools:
return _tool_config([t['name'] for t in tools['function_declarations']])
else:
return _tool_config([])
@asynccontextmanager
async def _make_request(
self,
messages: list[ModelMessage],
streamed: bool,
model_settings: GeminiModelSettings,
model_request_parameters: ModelRequestParameters,
) -> AsyncIterator[HTTPResponse]:
tools = self._get_tools(model_request_parameters)
tool_config = self._get_tool_config(model_request_parameters, tools)
sys_prompt_parts, contents = await self._message_to_gemini_content(messages)
request_data = _GeminiRequest(contents=contents)
if sys_prompt_parts:
request_data['systemInstruction'] = _GeminiTextContent(role='user', parts=sys_prompt_parts)
if tools is not None:
request_data['tools'] = tools
if tool_config is not None:
request_data['toolConfig'] = tool_config
generation_config: _GeminiGenerationConfig = {}
if model_settings:
if (max_tokens := model_settings.get('max_tokens')) is not None:
generation_config['max_output_tokens'] = max_tokens
if (temperature := model_settings.get('temperature')) is not None:
generation_config['temperature'] = temperature
if (top_p := model_settings.get('top_p')) is not None:
generation_config['top_p'] = top_p
if (presence_penalty := model_settings.get('presence_penalty')) is not None:
generation_config['presence_penalty'] = presence_penalty
if (frequency_penalty := model_settings.get('frequency_penalty')) is not None:
generation_config['frequency_penalty'] = frequency_penalty
if (gemini_safety_settings := model_settings.get('gemini_safety_settings')) != []:
request_data['safetySettings'] = gemini_safety_settings
if generation_config:
request_data['generationConfig'] = generation_config
headers = {'Content-Type': 'application/json', 'User-Agent': get_user_agent()}
url = f'/{self._model_name}:{"streamGenerateContent" if streamed else "generateContent"}'
request_json = _gemini_request_ta.dump_json(request_data, by_alias=True)
async with self.client.stream(
'POST',
url,
content=request_json,
headers=headers,
timeout=model_settings.get('timeout', USE_CLIENT_DEFAULT),
) as r:
if (status_code := r.status_code) != 200:
await r.aread()
if status_code >= 400:
raise ModelHTTPError(status_code=status_code, model_name=self.model_name, body=r.text)
raise UnexpectedModelBehavior(f'Unexpected response from gemini {status_code}', r.text)
yield r
def _process_response(self, response: _GeminiResponse) -> ModelResponse:
if len(response['candidates']) != 1:
raise UnexpectedModelBehavior('Expected exactly one candidate in Gemini response')
if 'content' not in response['candidates'][0]:
if response['candidates'][0].get('finish_reason') == 'SAFETY':
raise UnexpectedModelBehavior('Safety settings triggered', str(response))
else:
raise UnexpectedModelBehavior('Content field missing from Gemini response', str(response))
parts = response['candidates'][0]['content']['parts']
return _process_response_from_parts(parts, model_name=response.get('model_version', self._model_name))
async def _process_streamed_response(self, http_response: HTTPResponse) -> StreamedResponse:
"""Process a streamed response, and prepare a streaming response to return."""
aiter_bytes = http_response.aiter_bytes()
start_response: _GeminiResponse | None = None
content = bytearray()
async for chunk in aiter_bytes:
content.extend(chunk)
responses = _gemini_streamed_response_ta.validate_json(
_ensure_decodeable(content),
experimental_allow_partial='trailing-strings',
)
if responses:
last = responses[-1]
if last['candidates'] and last['candidates'][0].get('content', {}).get('parts'):
start_response = last
break
if start_response is None:
raise UnexpectedModelBehavior('Streamed response ended without content or tool calls')
return GeminiStreamedResponse(_model_name=self._model_name, _content=content, _stream=aiter_bytes)
async def _message_to_gemini_content(
self, messages: list[ModelMessage]
) -> tuple[list[_GeminiTextPart], list[_GeminiContent]]:
sys_prompt_parts: list[_GeminiTextPart] = []
contents: list[_GeminiContent] = []
for m in messages:
if isinstance(m, ModelRequest):
message_parts: list[_GeminiPartUnion] = []
for part in m.parts:
if isinstance(part, SystemPromptPart):
sys_prompt_parts.append(_GeminiTextPart(text=part.content))
elif isinstance(part, UserPromptPart):
message_parts.extend(await self._map_user_prompt(part))
elif isinstance(part, ToolReturnPart):
message_parts.append(_response_part_from_response(part.tool_name, part.model_response_object()))
elif isinstance(part, RetryPromptPart):
if part.tool_name is None:
message_parts.append(_GeminiTextPart(text=part.model_response()))
else:
response = {'call_error': part.model_response()}
message_parts.append(_response_part_from_response(part.tool_name, response))
else:
assert_never(part)
if message_parts:
contents.append(_GeminiContent(role='user', parts=message_parts))
elif isinstance(m, ModelResponse):
contents.append(_content_model_response(m))
else:
assert_never(m)
if instructions := self._get_instructions(messages):
sys_prompt_parts.insert(0, _GeminiTextPart(text=instructions))
return sys_prompt_parts, contents
async def _map_user_prompt(self, part: UserPromptPart) -> list[_GeminiPartUnion]:
if isinstance(part.content, str):
return [{'text': part.content}]
else:
content: list[_GeminiPartUnion] = []
for item in part.content:
if isinstance(item, str):
content.append({'text': item})
elif isinstance(item, BinaryContent):
base64_encoded = base64.b64encode(item.data).decode('utf-8')
content.append(
_GeminiInlineDataPart(inline_data={'data': base64_encoded, 'mime_type': item.media_type})
)
elif isinstance(item, (AudioUrl, ImageUrl, DocumentUrl, VideoUrl)):
client = cached_async_http_client()
response = await client.get(item.url, follow_redirects=True)
response.raise_for_status()
mime_type = response.headers['Content-Type'].split(';')[0]
inline_data = _GeminiInlineDataPart(
inline_data={'data': base64.b64encode(response.content).decode('utf-8'), 'mime_type': mime_type}
)
content.append(inline_data)
else:
assert_never(item)
return content
class AuthProtocol(Protocol):
"""Abstract definition for Gemini authentication."""
async def headers(self) -> dict[str, str]: ...
@dataclass
class ApiKeyAuth:
"""Authentication using an API key for the `X-Goog-Api-Key` header."""
api_key: str
async def headers(self) -> dict[str, str]:
# https://cloud.google.com/docs/authentication/api-keys-use#using-with-rest
return {'X-Goog-Api-Key': self.api_key}
@dataclass
class GeminiStreamedResponse(StreamedResponse):
"""Implementation of `StreamedResponse` for the Gemini model."""
_model_name: GeminiModelName
_content: bytearray
_stream: AsyncIterator[bytes]
_timestamp: datetime = field(default_factory=_utils.now_utc, init=False)
async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
async for gemini_response in self._get_gemini_responses():
candidate = gemini_response['candidates'][0]
if 'content' not in candidate:
raise UnexpectedModelBehavior('Streamed response has no content field')
gemini_part: _GeminiPartUnion
for gemini_part in candidate['content']['parts']:
if 'text' in gemini_part:
# Using vendor_part_id=None means we can produce multiple text parts if their deltas are sprinkled
# amongst the tool call deltas
yield self._parts_manager.handle_text_delta(vendor_part_id=None, content=gemini_part['text'])
elif 'function_call' in gemini_part:
# Here, we assume all function_call parts are complete and don't have deltas.
# We do this by assigning a unique randomly generated "vendor_part_id".
# We need to confirm whether this is actually true, but if it isn't, we can still handle it properly
# it would just be a bit more complicated. And we'd need to confirm the intended semantics.
maybe_event = self._parts_manager.handle_tool_call_delta(
vendor_part_id=uuid4(),
tool_name=gemini_part['function_call']['name'],
args=gemini_part['function_call']['args'],
tool_call_id=None,
)
if maybe_event is not None:
yield maybe_event
else:
assert 'function_response' in gemini_part, f'Unexpected part: {gemini_part}'
async def _get_gemini_responses(self) -> AsyncIterator[_GeminiResponse]:
# This method exists to ensure we only yield completed items, so we don't need to worry about
# partial gemini responses, which would make everything more complicated
gemini_responses: list[_GeminiResponse] = []
current_gemini_response_index = 0
# Right now, there are some circumstances where we will have information that could be yielded sooner than it is
# But changing that would make things a lot more complicated.
async for chunk in self._stream:
self._content.extend(chunk)
gemini_responses = _gemini_streamed_response_ta.validate_json(
_ensure_decodeable(self._content),
experimental_allow_partial='trailing-strings',
)
# The idea: yield only up to the latest response, which might still be partial.
# Note that if the latest response is complete, we could yield it immediately, but there's not a good
# allow_partial API to determine if the last item in the list is complete.
responses_to_yield = gemini_responses[:-1]
for r in responses_to_yield[current_gemini_response_index:]:
current_gemini_response_index += 1
self._usage += _metadata_as_usage(r)
yield r
# Now yield the final response, which should be complete
if gemini_responses:
r = gemini_responses[-1]
self._usage += _metadata_as_usage(r)
yield r
@property
def model_name(self) -> GeminiModelName:
"""Get the model name of the response."""
return self._model_name
@property
def timestamp(self) -> datetime:
"""Get the timestamp of the response."""
return self._timestamp
# We use typed dicts to define the Gemini API response schema
# once Pydantic partial validation supports, dataclasses, we could revert to using them
# TypeAdapters take care of validation and serialization
@pydantic.with_config(pydantic.ConfigDict(defer_build=True))
class _GeminiRequest(TypedDict):
"""Schema for an API request to the Gemini API.
See <https://ai.google.dev/api/generate-content#request-body> for API docs.
"""
# Note: Even though Google supposedly supports camelCase and snake_case, we've had user report misbehavior
# when using snake_case, which is why this typeddict now uses camelCase. And anyway, the plan is to replace this
# with an official google SDK in the near future anyway.
contents: list[_GeminiContent]
tools: NotRequired[_GeminiTools]
toolConfig: NotRequired[_GeminiToolConfig]
safetySettings: NotRequired[list[GeminiSafetySettings]]
systemInstruction: NotRequired[_GeminiTextContent]
"""
Developer generated system instructions, see
<https://ai.google.dev/gemini-api/docs/system-instructions?lang=rest>
"""
generationConfig: NotRequired[_GeminiGenerationConfig]
class GeminiSafetySettings(TypedDict):
"""Safety settings options for Gemini model request.
See [Gemini API docs](https://ai.google.dev/gemini-api/docs/safety-settings) for safety category and threshold descriptions.
For an example on how to use `GeminiSafetySettings`, see [here](../../agents.md#model-specific-settings).
"""
category: Literal[
'HARM_CATEGORY_UNSPECIFIED',
'HARM_CATEGORY_HARASSMENT',
'HARM_CATEGORY_HATE_SPEECH',
'HARM_CATEGORY_SEXUALLY_EXPLICIT',
'HARM_CATEGORY_DANGEROUS_CONTENT',
'HARM_CATEGORY_CIVIC_INTEGRITY',
]
"""
Safety settings category.
"""
threshold: Literal[
'HARM_BLOCK_THRESHOLD_UNSPECIFIED',
'BLOCK_LOW_AND_ABOVE',
'BLOCK_MEDIUM_AND_ABOVE',
'BLOCK_ONLY_HIGH',
'BLOCK_NONE',
'OFF',
]
"""
Safety settings threshold.
"""
class _GeminiGenerationConfig(TypedDict, total=False):
"""Schema for an API request to the Gemini API.
Note there are many additional fields available that have not been added yet.
See <https://ai.google.dev/api/generate-content#generationconfig> for API docs.
"""
max_output_tokens: int
temperature: float
top_p: float
presence_penalty: float
frequency_penalty: float
stop_sequences: list[str]
class _GeminiContent(TypedDict):
role: Literal['user', 'model']
parts: list[_GeminiPartUnion]
def _content_model_response(m: ModelResponse) -> _GeminiContent:
parts: list[_GeminiPartUnion] = []
for item in m.parts:
if isinstance(item, ToolCallPart):
parts.append(_function_call_part_from_call(item))
elif isinstance(item, TextPart):
if item.content:
parts.append(_GeminiTextPart(text=item.content))
else:
assert_never(item)
return _GeminiContent(role='model', parts=parts)
class _GeminiTextPart(TypedDict):
text: str
class _GeminiInlineData(TypedDict):
data: str
mime_type: Annotated[str, pydantic.Field(alias='mimeType')]
class _GeminiInlineDataPart(TypedDict):
"""See <https://ai.google.dev/api/caching#Blob>."""
inline_data: Annotated[_GeminiInlineData, pydantic.Field(alias='inlineData')]
class _GeminiFileData(TypedDict):
"""See <https://ai.google.dev/api/caching#FileData>."""
file_uri: Annotated[str, pydantic.Field(alias='fileUri')]
mime_type: Annotated[str, pydantic.Field(alias='mimeType')]
class _GeminiFileDataPart(TypedDict):
file_data: Annotated[_GeminiFileData, pydantic.Field(alias='fileData')]
class _GeminiFunctionCallPart(TypedDict):
function_call: Annotated[_GeminiFunctionCall, pydantic.Field(alias='functionCall')]
def _function_call_part_from_call(tool: ToolCallPart) -> _GeminiFunctionCallPart:
return _GeminiFunctionCallPart(function_call=_GeminiFunctionCall(name=tool.tool_name, args=tool.args_as_dict()))
def _process_response_from_parts(
parts: Sequence[_GeminiPartUnion], model_name: GeminiModelName, timestamp: datetime | None = None
) -> ModelResponse:
items: list[ModelResponsePart] = []
for part in parts:
if 'text' in part:
items.append(TextPart(content=part['text']))
elif 'function_call' in part:
items.append(ToolCallPart(tool_name=part['function_call']['name'], args=part['function_call']['args']))
elif 'function_response' in part:
raise UnexpectedModelBehavior(
f'Unsupported response from Gemini, expected all parts to be function calls or text, got: {part!r}'
)
return ModelResponse(parts=items, model_name=model_name, timestamp=timestamp or _utils.now_utc())
class _GeminiFunctionCall(TypedDict):
"""See <https://ai.google.dev/api/caching#FunctionCall>."""
name: str
args: dict[str, Any]
class _GeminiFunctionResponsePart(TypedDict):
function_response: Annotated[_GeminiFunctionResponse, pydantic.Field(alias='functionResponse')]
def _response_part_from_response(name: str, response: dict[str, Any]) -> _GeminiFunctionResponsePart:
return _GeminiFunctionResponsePart(function_response=_GeminiFunctionResponse(name=name, response=response))
class _GeminiFunctionResponse(TypedDict):
"""See <https://ai.google.dev/api/caching#FunctionResponse>."""
name: str
response: dict[str, Any]
def _part_discriminator(v: Any) -> str:
if isinstance(v, dict):
if 'text' in v:
return 'text'
elif 'inlineData' in v:
return 'inline_data'
elif 'fileData' in v:
return 'file_data'
elif 'functionCall' in v or 'function_call' in v:
return 'function_call'
elif 'functionResponse' in v or 'function_response' in v:
return 'function_response'
return 'text'
# See <https://ai.google.dev/api/caching#Part>
# we don't currently support other part types
# TODO discriminator
_GeminiPartUnion = Annotated[
Union[
Annotated[_GeminiTextPart, pydantic.Tag('text')],
Annotated[_GeminiFunctionCallPart, pydantic.Tag('function_call')],
Annotated[_GeminiFunctionResponsePart, pydantic.Tag('function_response')],
Annotated[_GeminiInlineDataPart, pydantic.Tag('inline_data')],
Annotated[_GeminiFileDataPart, pydantic.Tag('file_data')],
],
pydantic.Discriminator(_part_discriminator),
]
class _GeminiTextContent(TypedDict):
role: Literal['user', 'model']
parts: list[_GeminiTextPart]
class _GeminiTools(TypedDict):
function_declarations: Annotated[list[_GeminiFunction], pydantic.Field(alias='functionDeclarations')]
class _GeminiFunction(TypedDict):
name: str
description: str
parameters: NotRequired[dict[str, Any]]
"""
ObjectJsonSchema isn't really true since Gemini only accepts a subset of JSON Schema
<https://ai.google.dev/gemini-api/docs/function-calling#function_declarations>
and
<https://ai.google.dev/api/caching#FunctionDeclaration>
"""
def _function_from_abstract_tool(tool: ToolDefinition) -> _GeminiFunction:
json_schema = tool.parameters_json_schema
f = _GeminiFunction(name=tool.name, description=tool.description)
if json_schema.get('properties'):
f['parameters'] = json_schema
return f
class _GeminiToolConfig(TypedDict):
function_calling_config: _GeminiFunctionCallingConfig
def _tool_config(function_names: list[str]) -> _GeminiToolConfig:
return _GeminiToolConfig(
function_calling_config=_GeminiFunctionCallingConfig(mode='ANY', allowed_function_names=function_names)
)
class _GeminiFunctionCallingConfig(TypedDict):
mode: Literal['ANY', 'AUTO']
allowed_function_names: list[str]
@pydantic.with_config(pydantic.ConfigDict(defer_build=True))
class _GeminiResponse(TypedDict):
"""Schema for the response from the Gemini API.
See <https://ai.google.dev/api/generate-content#v1beta.GenerateContentResponse>
and <https://cloud.google.com/vertex-ai/docs/reference/rest/v1/GenerateContentResponse>
"""
candidates: list[_GeminiCandidates]
# usageMetadata appears to be required by both APIs but is omitted when streaming responses until the last response
usage_metadata: NotRequired[Annotated[_GeminiUsageMetaData, pydantic.Field(alias='usageMetadata')]]
prompt_feedback: NotRequired[Annotated[_GeminiPromptFeedback, pydantic.Field(alias='promptFeedback')]]
model_version: NotRequired[Annotated[str, pydantic.Field(alias='modelVersion')]]
class _GeminiCandidates(TypedDict):
"""See <https://ai.google.dev/api/generate-content#v1beta.Candidate>."""
content: NotRequired[_GeminiContent]
finish_reason: NotRequired[Annotated[Literal['STOP', 'MAX_TOKENS', 'SAFETY'], pydantic.Field(alias='finishReason')]]
"""
See <https://ai.google.dev/api/generate-content#FinishReason>, lots of other values are possible,
but let's wait until we see them and know what they mean to add them here.
"""
avg_log_probs: NotRequired[Annotated[float, pydantic.Field(alias='avgLogProbs')]]
index: NotRequired[int]
safety_ratings: NotRequired[Annotated[list[_GeminiSafetyRating], pydantic.Field(alias='safetyRatings')]]
class _GeminiUsageMetaData(TypedDict, total=False):
"""See <https://ai.google.dev/api/generate-content#FinishReason>.
The docs suggest all fields are required, but some are actually not required, so we assume they are all optional.
"""
prompt_token_count: Annotated[int, pydantic.Field(alias='promptTokenCount')]
candidates_token_count: NotRequired[Annotated[int, pydantic.Field(alias='candidatesTokenCount')]]
total_token_count: Annotated[int, pydantic.Field(alias='totalTokenCount')]
cached_content_token_count: NotRequired[Annotated[int, pydantic.Field(alias='cachedContentTokenCount')]]
def _metadata_as_usage(response: _GeminiResponse) -> usage.Usage:
metadata = response.get('usage_metadata')
if metadata is None:
return usage.Usage()
details: dict[str, int] = {}
if cached_content_token_count := metadata.get('cached_content_token_count'):
details['cached_content_token_count'] = cached_content_token_count
return usage.Usage(
request_tokens=metadata.get('prompt_token_count', 0),
response_tokens=metadata.get('candidates_token_count', 0),
total_tokens=metadata.get('total_token_count', 0),
details=details,
)
class _GeminiSafetyRating(TypedDict):
"""See <https://ai.google.dev/gemini-api/docs/safety-settings#safety-filters>."""
category: Literal[
'HARM_CATEGORY_HARASSMENT',
'HARM_CATEGORY_HATE_SPEECH',
'HARM_CATEGORY_SEXUALLY_EXPLICIT',
'HARM_CATEGORY_DANGEROUS_CONTENT',
'HARM_CATEGORY_CIVIC_INTEGRITY',
]
probability: Literal['NEGLIGIBLE', 'LOW', 'MEDIUM', 'HIGH']
blocked: NotRequired[bool]
class _GeminiPromptFeedback(TypedDict):
"""See <https://ai.google.dev/api/generate-content#v1beta.GenerateContentResponse>."""
block_reason: Annotated[str, pydantic.Field(alias='blockReason')]
safety_ratings: Annotated[list[_GeminiSafetyRating], pydantic.Field(alias='safetyRatings')]
_gemini_request_ta = pydantic.TypeAdapter(_GeminiRequest)
_gemini_response_ta = pydantic.TypeAdapter(_GeminiResponse)
# steam requests return a list of https://ai.google.dev/api/generate-content#method:-models.streamgeneratecontent
_gemini_streamed_response_ta = pydantic.TypeAdapter(list[_GeminiResponse], config=pydantic.ConfigDict(defer_build=True))
class _GeminiJsonSchema(WalkJsonSchema):
"""Transforms the JSON Schema from Pydantic to be suitable for Gemini.
Gemini which [supports](https://ai.google.dev/gemini-api/docs/function-calling#function_declarations)
a subset of OpenAPI v3.0.3.
Specifically:
* gemini doesn't allow the `title` keyword to be set
* gemini doesn't allow `$defs` — we need to inline the definitions where possible
"""
def __init__(self, schema: JsonSchema):
super().__init__(schema, prefer_inlined_defs=True, simplify_nullable_unions=True)
def transform(self, schema: JsonSchema) -> JsonSchema:
# Note: we need to remove `additionalProperties: False` since it is currently mishandled by Gemini
additional_properties = schema.pop(
'additionalProperties', None
) # don't pop yet so it's included in the warning
if additional_properties: # pragma: no cover
original_schema = {**schema, 'additionalProperties': additional_properties}
warnings.warn(
'`additionalProperties` is not supported by Gemini; it will be removed from the tool JSON schema.'
f' Full schema: {self.schema}\n\n'
f'Source of additionalProperties within the full schema: {original_schema}\n\n'
'If this came from a field with a type like `dict[str, MyType]`, that field will always be empty.\n\n'
"If Google's APIs are updated to support this properly, please create an issue on the PydanticAI GitHub"
' and we will fix this behavior.',
UserWarning,
)
schema.pop('title', None)
schema.pop('default', None)
schema.pop('$schema', None)
if (const := schema.pop('const', None)) is not None: # pragma: no cover
# Gemini doesn't support const, but it does support enum with a single value
schema['enum'] = [const]
schema.pop('discriminator', None)
schema.pop('examples', None)
# TODO: Should we use the trick from pydantic_ai.models.openai._OpenAIJsonSchema
# where we add notes about these properties to the field description?
schema.pop('exclusiveMaximum', None)
schema.pop('exclusiveMinimum', None)
type_ = schema.get('type')
if 'oneOf' in schema and 'type' not in schema: # pragma: no cover
# This gets hit when we have a discriminated union
# Gemini returns an API error in this case even though it says in its error message it shouldn't...
# Changing the oneOf to an anyOf prevents the API error and I think is functionally equivalent
schema['anyOf'] = schema.pop('oneOf')
if type_ == 'string' and (fmt := schema.pop('format', None)):
description = schema.get('description')
if description:
schema['description'] = f'{description} (format: {fmt})'
else:
schema['description'] = f'Format: {fmt}'
if '$ref' in schema:
raise UserError(f'Recursive `$ref`s in JSON Schema are not supported by Gemini: {schema["$ref"]}')
if 'prefixItems' in schema:
# prefixItems is not currently supported in Gemini, so we convert it to items for best compatibility
prefix_items = schema.pop('prefixItems')
items = schema.get('items')
unique_items = [items] if items is not None else []
for item in prefix_items:
if item not in unique_items:
unique_items.append(item)
if len(unique_items) > 1: # pragma: no cover
schema['items'] = {'anyOf': unique_items}
elif len(unique_items) == 1:
schema['items'] = unique_items[0]
schema.setdefault('minItems', len(prefix_items))
if items is None:
schema.setdefault('maxItems', len(prefix_items))
return schema
def _ensure_decodeable(content: bytearray) -> bytearray:
"""Trim any invalid unicode point bytes off the end of a bytearray.
This is necessary before attempting to parse streaming JSON bytes.
This is a temporary workaround until https://github.com/pydantic/pydantic-core/issues/1633 is resolved
"""
while True:
try:
content.decode()
except UnicodeDecodeError:
content = content[:-1] # this will definitely succeed before we run out of bytes
else:
return content