Skip to content

pydantic_ai.models.google

Interface that uses the google-genai package under the hood to access Google's Gemini models via both the Gemini API and Google Cloud (formerly known as Vertex AI).

Setup

For details on how to set up authentication with this model, see model configuration for Google.

LatestGoogleModelNames module-attribute

LatestGoogleModelNames = Literal[
    "gemini-flash-latest",
    "gemini-flash-lite-latest",
    "gemini-2.0-flash",
    "gemini-2.0-flash-lite",
    "gemini-2.5-flash",
    "gemini-2.5-flash-preview-09-2025",
    "gemini-2.5-flash-image",
    "gemini-2.5-flash-lite",
    "gemini-2.5-flash-lite-preview-09-2025",
    "gemini-2.5-pro",
    "gemini-3-flash-preview",
    "gemini-3-pro-image-preview",
    "gemini-3-pro-preview",
    "gemini-3.1-flash-image-preview",
    "gemini-3.1-flash-lite-preview",
    "gemini-3.1-pro-preview",
    "gemini-3.5-flash",
]

Latest Gemini models.

GoogleModelName module-attribute

GoogleModelName = str | LatestGoogleModelNames

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 for a full list.

GoogleCloudServiceTier module-attribute

GoogleCloudServiceTier = Literal[
    "pt_then_on_demand",
    "pt_only",
    "pt_then_flex",
    "pt_then_priority",
    "on_demand",
    "flex_only",
    "priority_only",
]

Values for the google_cloud_service_tier field on GoogleModelSettings.

Controls Google Cloud HTTP headers for Provisioned Throughput (PT), Flex PayGo, and Priority PayGo.

  • 'pt_then_on_demand' (default): PT when quota allows, then standard on-demand spillover. No headers sent.
  • 'pt_only': PT only (X-Vertex-AI-LLM-Request-Type: dedicated). No on-demand spillover; returns 429 when over quota.
  • 'pt_then_flex': PT when quota allows, then Flex PayGo spillover (X-Vertex-AI-LLM-Shared-Request-Type: flex).
  • 'pt_then_priority': PT when quota allows, then Priority PayGo spillover (X-Vertex-AI-LLM-Shared-Request-Type: priority).
  • 'on_demand': Standard on-demand only (X-Vertex-AI-LLM-Request-Type: shared). Bypasses PT for this request.
  • 'flex_only': Flex PayGo only (X-Vertex-AI-LLM-Request-Type: shared and X-Vertex-AI-LLM-Shared-Request-Type: flex). Bypasses PT.
  • 'priority_only': Priority PayGo only (X-Vertex-AI-LLM-Request-Type: shared and X-Vertex-AI-LLM-Shared-Request-Type: priority). Bypasses PT.

Not every model or region supports every value; see the linked Google docs.

GoogleModelSettings

Bases: ModelSettings

Settings used for a Gemini model request.

Source code in pydantic_ai_slim/pydantic_ai/models/google.py
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
class GoogleModelSettings(ModelSettings, total=False):
    """Settings used for a Gemini model request."""

    # ALL FIELDS MUST BE `google_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.

    google_safety_settings: list[SafetySettingDict]
    """The safety settings to use for the model.

    See <https://ai.google.dev/gemini-api/docs/safety-settings> for more information.
    """

    google_thinking_config: ThinkingConfigDict
    """The thinking configuration to use for the model.

    See <https://ai.google.dev/gemini-api/docs/thinking> for more information.
    """

    google_labels: dict[str, str]
    """User-defined metadata to break down billed charges. Only supported by the Vertex AI API.

    See the [Gemini API docs](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/add-labels-to-api-calls) for use cases and limitations.
    """

    google_video_resolution: MediaResolution
    """The video resolution to use for the model.

    See <https://ai.google.dev/api/generate-content#MediaResolution> for more information.
    """

    google_cached_content: str
    """The name of the cached content to use for the model.

    See <https://ai.google.dev/gemini-api/docs/caching> for more information.
    """

    google_logprobs: bool
    """Include log probabilities in the response.

    See <https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens> for more information.

    Note: Only supported for Vertex AI and non-streaming requests.

    These will be included in `ModelResponse.provider_details['logprobs']`.
    """

    google_top_logprobs: int
    """Include log probabilities of the top n tokens in the response.

    See <https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens> for more information.

    Note: Only supported for Vertex AI and non-streaming requests.

    These will be included in `ModelResponse.provider_details['logprobs']`.
    """

    google_cloud_service_tier: GoogleCloudServiceTier
    """The service tier to use for the model request when using Google Cloud.

    Controls routing for Provisioned Throughput, Flex PayGo, and Priority PayGo
    (e.g., `'pt_only'`, `'flex_only'`, `'priority_only'`).

    See [`GoogleCloudServiceTier`][pydantic_ai.models.google.GoogleCloudServiceTier] for all values,
    headers sent, and links to Google docs.
    """

google_safety_settings instance-attribute

google_safety_settings: list[SafetySettingDict]

The safety settings to use for the model.

See https://ai.google.dev/gemini-api/docs/safety-settings for more information.

google_thinking_config instance-attribute

google_thinking_config: ThinkingConfigDict

The thinking configuration to use for the model.

See https://ai.google.dev/gemini-api/docs/thinking for more information.

google_labels instance-attribute

google_labels: dict[str, str]

User-defined metadata to break down billed charges. Only supported by the Vertex AI API.

See the Gemini API docs for use cases and limitations.

google_video_resolution instance-attribute

google_video_resolution: MediaResolution

The video resolution to use for the model.

See https://ai.google.dev/api/generate-content#MediaResolution for more information.

google_cached_content instance-attribute

google_cached_content: str

The name of the cached content to use for the model.

See https://ai.google.dev/gemini-api/docs/caching for more information.

google_logprobs instance-attribute

google_logprobs: bool

Include log probabilities in the response.

See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens for more information.

Note: Only supported for Vertex AI and non-streaming requests.

These will be included in ModelResponse.provider_details['logprobs'].

google_top_logprobs instance-attribute

google_top_logprobs: int

Include log probabilities of the top n tokens in the response.

See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/content-generation-parameters#log-probabilities-output-tokens for more information.

Note: Only supported for Vertex AI and non-streaming requests.

These will be included in ModelResponse.provider_details['logprobs'].

google_cloud_service_tier instance-attribute

google_cloud_service_tier: GoogleCloudServiceTier

The service tier to use for the model request when using Google Cloud.

Controls routing for Provisioned Throughput, Flex PayGo, and Priority PayGo (e.g., 'pt_only', 'flex_only', 'priority_only').

See GoogleCloudServiceTier for all values, headers sent, and links to Google docs.

GoogleModel dataclass

Bases: Model[Client]

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.

Apart from __init__, all methods are private or match those of the base class.

Source code in pydantic_ai_slim/pydantic_ai/models/google.py
 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
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
@dataclass(init=False)
class GoogleModel(Model[Client]):
    """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.
    """

    _model_name: GoogleModelName = field(repr=False)
    _provider: Provider[Client] = field(repr=False)

    def __init__(
        self,
        model_name: GoogleModelName,
        *,
        provider: Literal['google', 'google-cloud', 'gateway'] | Provider[Client] = 'google',
        profile: ModelProfileSpec | None = None,
        settings: ModelSettings | None = None,
    ):
        """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' (Gemini API) or 'google-cloud' (Google Cloud, formerly known as Vertex AI),
                or an instance of `Provider[google.genai.AsyncClient]`. Defaults to 'google'.
            profile: The model profile to use. Defaults to a profile picked by the provider based on the model name.
            settings: The model settings to use. Defaults to None.
        """
        self._model_name = model_name

        if isinstance(provider, str):
            provider = infer_provider('gateway/google-cloud' if provider == 'gateway' else provider)
        self._provider = provider

        super().__init__(settings=settings, profile=profile)

    @property
    def client(self) -> Client:
        return self._provider.client

    @property
    def base_url(self) -> str:
        return self._provider.base_url

    @property
    def model_name(self) -> GoogleModelName:
        """The model name."""
        return self._model_name

    @property
    def system(self) -> str:
        """The model provider."""
        return self._provider.name

    @property
    def _matching_provider_names(self) -> frozenset[str]:
        if self.system in _GOOGLE_CLOUD_PROVIDER_NAMES:
            return _GOOGLE_CLOUD_PROVIDER_NAMES
        if self.system in _GEMINI_API_PROVIDER_NAMES:
            return _GEMINI_API_PROVIDER_NAMES
        return frozenset({self.system})  # pragma: no cover

    @cached_property
    def profile(self) -> GoogleModelProfile:
        return cast(GoogleModelProfile, super().profile)

    @classmethod
    def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]]:
        """Return the set of native tool types this model can handle."""
        return frozenset({WebSearchTool, CodeExecutionTool, FileSearchTool, WebFetchTool, ImageGenerationTool})

    def prepare_request(
        self, model_settings: ModelSettings | None, model_request_parameters: ModelRequestParameters
    ) -> tuple[ModelSettings | None, ModelRequestParameters]:
        # Ignore optional infrastructure native tools (e.g. auto-injected `ToolSearchTool`) —
        # they're dropped by `Model.prepare_request` when inert and shouldn't trigger the
        # "native tool + output tools" path.
        user_native_tools = [t for t in model_request_parameters.native_tools if not t.optional]
        if (
            user_native_tools
            and model_request_parameters.output_tools
            and not self.profile.get('google_supports_tool_combination', False)
        ):
            # Pre-Gemini-3 models reject `output_tools + native_tools` together. Force prompted
            # output (the only mode that doesn't add a tool to the request); raise if the caller
            # explicitly asked for tool/native output.
            model_request_parameters = model_request_parameters.with_default_output_mode('prompted')
            if model_request_parameters.output_mode != 'prompted':
                raise UserError(
                    'This model does not support output tools and built-in tools at the same time. '
                    'Use `output_type=PromptedOutput(...)` instead.'
                )
        return super().prepare_request(model_settings, model_request_parameters)

    async def request(
        self,
        messages: list[ModelMessage],
        model_settings: ModelSettings | None,
        model_request_parameters: ModelRequestParameters,
    ) -> ModelResponse:
        check_allow_model_requests()
        model_settings, model_request_parameters = self.prepare_request(
            model_settings,
            model_request_parameters,
        )
        model_settings = cast(GoogleModelSettings, model_settings or {})
        response = await self._generate_content(messages, False, model_settings, model_request_parameters)
        return self._process_response(response)

    async def count_tokens(
        self,
        messages: list[ModelMessage],
        model_settings: ModelSettings | None,
        model_request_parameters: ModelRequestParameters,
    ) -> usage.RequestUsage:
        check_allow_model_requests()
        model_settings, model_request_parameters = self.prepare_request(
            model_settings,
            model_request_parameters,
        )
        model_settings = cast(GoogleModelSettings, model_settings or {})
        contents, generation_config = await self._build_content_and_config(
            messages, model_settings, model_request_parameters
        )

        # Annoyingly, the type of `GenerateContentConfigDict.get` is "partially `Unknown`" because `response_schema` includes `typing._UnionGenericAlias`,
        # so without this we'd need `pyright: ignore[reportUnknownMemberType]` on every line and wouldn't get type checking anyway.
        generation_config = cast(dict[str, Any], generation_config)

        config = CountTokensConfigDict(
            http_options=generation_config.get('http_options'),
        )
        if self._provider.name not in _GEMINI_API_PROVIDER_NAMES:
            # The fields are not supported by the Gemini API per https://github.com/googleapis/python-genai/blob/7e4ec284dc6e521949626f3ed54028163ef9121d/google/genai/models.py#L1195-L1214
            config.update(  # pragma: lax no cover
                system_instruction=generation_config.get('system_instruction'),
                tools=cast(list[ToolDict], generation_config.get('tools')),
                # Annoyingly, GenerationConfigDict has fewer fields than GenerateContentConfigDict, and no extra fields are allowed.
                generation_config=GenerationConfigDict(
                    temperature=generation_config.get('temperature'),
                    top_p=generation_config.get('top_p'),
                    max_output_tokens=generation_config.get('max_output_tokens'),
                    stop_sequences=generation_config.get('stop_sequences'),
                    presence_penalty=generation_config.get('presence_penalty'),
                    frequency_penalty=generation_config.get('frequency_penalty'),
                    seed=generation_config.get('seed'),
                    thinking_config=generation_config.get('thinking_config'),
                    media_resolution=generation_config.get('media_resolution'),
                    response_mime_type=generation_config.get('response_mime_type'),
                    response_json_schema=generation_config.get('response_json_schema'),
                ),
            )

        response = await self.client.aio.models.count_tokens(
            model=self._model_name,
            contents=contents,
            config=config,
        )
        if response.total_tokens is None:
            raise UnexpectedModelBehavior(  # pragma: no cover
                'Total tokens missing from Gemini response', str(response)
            )
        return usage.RequestUsage(
            input_tokens=response.total_tokens,
        )

    @asynccontextmanager
    async def request_stream(
        self,
        messages: list[ModelMessage],
        model_settings: ModelSettings | None,
        model_request_parameters: ModelRequestParameters,
        run_context: RunContext[Any] | None = None,
    ) -> AsyncIterator[StreamedResponse]:
        check_allow_model_requests()
        model_settings, model_request_parameters = self.prepare_request(
            model_settings,
            model_request_parameters,
        )
        model_settings = cast(GoogleModelSettings, model_settings or {})
        response = await self._generate_content(messages, True, model_settings, model_request_parameters)
        try:
            yield await self._process_streamed_response(response, model_request_parameters)  # type: ignore
        finally:
            aclose = getattr(response, 'aclose', None)
            if aclose is not None:  # pragma: no branch
                await aclose()

    def _build_image_config(self, tool: ImageGenerationTool) -> ImageConfigDict:
        """Build ImageConfigDict from ImageGenerationTool with validation."""
        image_config = ImageConfigDict()

        if tool.aspect_ratio is not None:
            image_config['aspect_ratio'] = tool.aspect_ratio

        if tool.size is not None:
            if tool.size not in _GOOGLE_IMAGE_SIZES:
                raise UserError(
                    f'Google image generation only supports `size` values: {_GOOGLE_IMAGE_SIZES}. '
                    f'Got: {tool.size!r}. Omit `size` to use the default (1K).'
                )
            image_config['image_size'] = tool.size

        if self.system in _GOOGLE_CLOUD_PROVIDER_NAMES:
            if tool.output_format is not None:
                if tool.output_format not in _GOOGLE_IMAGE_OUTPUT_FORMATS:
                    raise UserError(
                        f'Google image generation only supports `output_format` values: {_GOOGLE_IMAGE_OUTPUT_FORMATS}. '
                        f'Got: {tool.output_format!r}.'
                    )
                image_config['output_mime_type'] = f'image/{tool.output_format}'

            output_compression = tool.output_compression
            if output_compression is not None:
                if not (0 <= output_compression <= 100):
                    raise UserError(
                        f'Google image generation `output_compression` must be between 0 and 100. '
                        f'Got: {output_compression}.'
                    )
                if tool.output_format not in (None, 'jpeg'):
                    raise UserError(
                        f'Google image generation `output_compression` is only supported for JPEG format. '
                        f'Got format: {tool.output_format!r}. Either set `output_format="jpeg"` or remove `output_compression`.'
                    )
                image_config['output_compression_quality'] = output_compression
                if tool.output_format is None:
                    image_config['output_mime_type'] = 'image/jpeg'

        return image_config

    def _get_native_tools(
        self, model_request_parameters: ModelRequestParameters
    ) -> tuple[list[ToolDict], ImageConfigDict | None]:
        """Get Google-specific native tools (web search, code execution, etc.).

        Returns:
            A tuple of (native_tools, image_config).
        """
        tools: list[ToolDict] = []
        image_config: ImageConfigDict | None = None
        if model_request_parameters.native_tools:
            if model_request_parameters.function_tools and not self.profile.get(
                'google_supports_tool_combination', False
            ):
                raise UserError('This model does not support function tools and built-in tools at the same time.')

            for tool in model_request_parameters.native_tools:
                if isinstance(tool, WebSearchTool):
                    tools.append(ToolDict(google_search=GoogleSearchDict()))
                elif isinstance(tool, WebFetchTool):
                    tools.append(ToolDict(url_context=UrlContextDict()))
                elif isinstance(tool, CodeExecutionTool):
                    tools.append(ToolDict(code_execution=ToolCodeExecutionDict()))
                elif isinstance(tool, FileSearchTool):
                    file_search_config = FileSearchDict(file_search_store_names=list(tool.file_store_ids))
                    tools.append(ToolDict(file_search=file_search_config))
                elif isinstance(tool, ImageGenerationTool):  # pragma: no branch
                    if not self.profile.get('supports_image_output', False):
                        raise UserError(
                            "`ImageGenerationTool` is not supported by this model. Use a model with 'image' in the name instead."
                        )
                    image_config = self._build_image_config(tool)
                else:  # pragma: no cover
                    raise UserError(
                        f'`{tool.__class__.__name__}` is not supported by `GoogleModel`. If it should be, please file an issue.'
                    )

        return tools, image_config

    def _get_tool_config(
        self,
        model_request_parameters: ModelRequestParameters,
        model_settings: GoogleModelSettings,
    ) -> tuple[list[ToolDict] | None, ToolConfigDict | None, ImageConfigDict | None]:
        """Determine which tools to send and the API tool config.

        Returns:
            A tuple of (filtered_tools, tool_config, image_config).
        """
        native_tools, image_config = self._get_native_tools(model_request_parameters)

        tool_defs = model_request_parameters.tool_defs

        resolved_tool_choice = resolve_tool_choice(model_settings, model_request_parameters)

        function_calling_config_modes: dict[ToolChoiceScalar, FunctionCallingConfigMode] = {
            'auto': FunctionCallingConfigMode.AUTO,
            'none': FunctionCallingConfigMode.NONE,
            'required': FunctionCallingConfigMode.ANY,
        }

        allowed_function_names: list[str] = []
        if isinstance(resolved_tool_choice, tuple):
            tool_choice_mode, tool_names = resolved_tool_choice
            if tool_choice_mode == 'auto':
                # Breaks caching, but Google doesn't support AUTO mode with allowed_function_names
                tool_defs = {k: v for k, v in tool_defs.items() if k in tool_names}
            else:
                # Use ANY mode with allowed_function_names to force one of the specified tools
                allowed_function_names = list(tool_names)
        else:
            tool_choice_mode = resolved_tool_choice

        function_calling_config: FunctionCallingConfigDict = {'mode': function_calling_config_modes[tool_choice_mode]}
        if allowed_function_names:
            function_calling_config['allowed_function_names'] = allowed_function_names
        tool_config = ToolConfigDict(function_calling_config=function_calling_config)

        # `include_server_side_tool_invocations` makes Gemini emit explicit `tool_call`/`tool_response`
        # parts for WebSearchTool, WebFetchTool, FileSearchTool. Pre-Gemini-3 models reject the field
        # ('Tool call context circulation is not enabled'); CodeExecutionTool uses its own
        # `executable_code`/`code_execution_result` parts; ImageGenerationTool runs through `image_config`.
        emits_tool_call_invocations = any(
            isinstance(t, (WebSearchTool, WebFetchTool, FileSearchTool)) for t in model_request_parameters.native_tools
        )
        if emits_tool_call_invocations and self.profile.get('google_supports_server_side_tool_invocations', False):
            tool_config['include_server_side_tool_invocations'] = True

        tools: list[ToolDict] = [
            ToolDict(function_declarations=[_function_declaration_from_tool(t)]) for t in tool_defs.values()
        ]

        tools.extend(native_tools)

        if not tools:
            return None, None, image_config

        return tools, tool_config, image_config

    @overload
    async def _generate_content(
        self,
        messages: list[ModelMessage],
        stream: Literal[False],
        model_settings: GoogleModelSettings,
        model_request_parameters: ModelRequestParameters,
    ) -> GenerateContentResponse: ...

    @overload
    async def _generate_content(
        self,
        messages: list[ModelMessage],
        stream: Literal[True],
        model_settings: GoogleModelSettings,
        model_request_parameters: ModelRequestParameters,
    ) -> Awaitable[AsyncIterator[GenerateContentResponse]]: ...

    async def _generate_content(
        self,
        messages: list[ModelMessage],
        stream: bool,
        model_settings: GoogleModelSettings,
        model_request_parameters: ModelRequestParameters,
    ) -> GenerateContentResponse | Awaitable[AsyncIterator[GenerateContentResponse]]:
        contents, config = await self._build_content_and_config(
            messages,
            model_settings,
            model_request_parameters,
        )
        func = self.client.aio.models.generate_content_stream if stream else self.client.aio.models.generate_content
        try:
            return await func(model=self._model_name, contents=contents, config=config)  # type: ignore
        except errors.APIError as e:
            if (status_code := e.code) >= 400:
                raise ModelHTTPError(
                    status_code=status_code,
                    model_name=self._model_name,
                    body=cast(Any, e.details),  # pyright: ignore[reportUnknownMemberType]
                ) from e
            raise ModelAPIError(model_name=self._model_name, message=str(e)) from e

    def _translate_thinking(
        self,
        model_settings: GoogleModelSettings,
        model_request_parameters: ModelRequestParameters,
    ) -> ThinkingConfigDict | None:
        """Get thinking config, falling back to unified thinking when provider-specific setting is not set."""
        if config := model_settings.get('google_thinking_config'):
            return config
        thinking = model_request_parameters.thinking
        if thinking is None:
            return None
        profile = self.profile
        if thinking is False:
            if profile.get('google_supports_thinking_level', False):
                return ThinkingConfigDict(thinking_level=cast(Any, 'MINIMAL'))
            return ThinkingConfigDict(thinking_budget=0)
        if profile.get('google_supports_thinking_level', False):
            if thinking is True:
                return ThinkingConfigDict(include_thoughts=True)
            level_map: dict[ThinkingEffort, str] = {
                'minimal': 'MINIMAL',
                'low': 'LOW',
                'medium': 'MEDIUM',
                'high': 'HIGH',
                'xhigh': 'HIGH',  # no higher level available
            }
            return ThinkingConfigDict(include_thoughts=True, thinking_level=cast(Any, level_map[thinking]))
        else:
            if thinking is True:
                return ThinkingConfigDict(include_thoughts=True)
            budget_map: dict[ThinkingEffort, int] = {
                'minimal': 128,  # minimum for Gemini 2.5 Pro
                'low': 2048,
                'medium': 8192,
                'high': 24576,
                'xhigh': 24576,  # max for Flash; Pro goes to 32768 but we use a safe common max
            }
            return ThinkingConfigDict(include_thoughts=True, thinking_budget=budget_map[thinking])

    async def _build_content_and_config(
        self,
        messages: list[ModelMessage],
        model_settings: GoogleModelSettings,
        model_request_parameters: ModelRequestParameters,
    ) -> tuple[list[ContentUnionDict], GenerateContentConfigDict]:
        tools, tool_config, image_config = self._get_tool_config(model_request_parameters, model_settings)
        if model_request_parameters.function_tools and not self.profile.get('supports_tools', True):
            raise UserError('Tools are not supported by this model.')

        response_mime_type = None
        response_schema = None
        if model_request_parameters.output_mode == 'native':
            if model_request_parameters.function_tools and not self.profile.get(
                'google_supports_tool_combination', False
            ):
                raise UserError(
                    'This model does not support `NativeOutput` and function tools at the same time. Use `output_type=ToolOutput(...)` instead.'
                )
            response_mime_type = 'application/json'
            output_object = model_request_parameters.output_object
            assert output_object is not None
            response_schema = self._map_response_schema(output_object)
        elif model_request_parameters.output_mode == 'prompted' and not tools:
            if not self.profile.get('supports_json_object_output', False):
                raise UserError('JSON output is not supported by this model.')
            response_mime_type = 'application/json'
        system_instruction, contents = await self._map_messages(messages, model_request_parameters)

        modalities: list[str] = [Modality.TEXT.value]
        if self.profile.get('supports_image_output', False):
            modalities.append(Modality.IMAGE.value)
            if not model_request_parameters.allow_text_output:
                modalities.remove(Modality.TEXT.value)

        headers: dict[str, str] = {'Content-Type': 'application/json', 'User-Agent': get_user_agent()}
        if extra_headers := model_settings.get('extra_headers'):
            headers.update(extra_headers)

        gla_service_tier: _GlaServiceTier | None = None
        if self.system in _GOOGLE_CLOUD_PROVIDER_NAMES:
            headers.update(_google_cloud_service_tier_headers(_resolve_google_cloud_service_tier(model_settings)))
        else:
            gla_service_tier = _resolve_gla_service_tier(model_settings)

        http_options: HttpOptionsDict = {'headers': headers}
        if timeout := model_settings.get('timeout'):
            if isinstance(timeout, int | float):
                http_options['timeout'] = int(1000 * timeout)
            else:
                raise UserError('Google does not support setting ModelSettings.timeout to a httpx.Timeout')

        config = GenerateContentConfigDict(
            http_options=http_options,
            system_instruction=system_instruction,
            temperature=model_settings.get('temperature'),
            top_p=model_settings.get('top_p'),
            max_output_tokens=model_settings.get('max_tokens'),
            stop_sequences=model_settings.get('stop_sequences'),
            presence_penalty=model_settings.get('presence_penalty'),
            frequency_penalty=model_settings.get('frequency_penalty'),
            seed=model_settings.get('seed'),
            safety_settings=model_settings.get('google_safety_settings'),
            thinking_config=self._translate_thinking(model_settings, model_request_parameters),
            labels=model_settings.get('google_labels'),
            media_resolution=model_settings.get('google_video_resolution'),
            cached_content=model_settings.get('google_cached_content'),
            tools=cast(ToolListUnionDict, tools),
            tool_config=tool_config,
            response_mime_type=response_mime_type,
            response_json_schema=response_schema,
            response_modalities=modalities,
            image_config=image_config,
        )

        if gla_service_tier is not None:
            config['service_tier'] = cast(_GoogleSDKServiceTier, gla_service_tier)

        # Validate logprobs settings
        logprobs_requested = model_settings.get('google_logprobs')
        if logprobs_requested:
            config['response_logprobs'] = True

            if 'google_top_logprobs' in model_settings:
                config['logprobs'] = model_settings.get('google_top_logprobs')

        return contents, config

    def _process_response(self, response: GenerateContentResponse) -> ModelResponse:
        candidate = response.candidates[0] if response.candidates else None

        provider_response_id = response.response_id
        finish_reason: FinishReason | None = None
        provider_details: dict[str, Any] = {}

        raw_finish_reason = candidate.finish_reason if candidate else None
        if raw_finish_reason and candidate:  # pragma: no branch
            provider_details = {'finish_reason': raw_finish_reason.value}
            # Add safety ratings to provider details
            if candidate.safety_ratings:
                provider_details['safety_ratings'] = [r.model_dump(by_alias=True) for r in candidate.safety_ratings]
            finish_reason = _FINISH_REASON_MAP.get(raw_finish_reason)
        elif candidate is None and response.prompt_feedback and response.prompt_feedback.block_reason:
            block_reason = response.prompt_feedback.block_reason
            provider_details['block_reason'] = block_reason.value
            if response.prompt_feedback.block_reason_message:
                provider_details['block_reason_message'] = response.prompt_feedback.block_reason_message
            if response.prompt_feedback.safety_ratings:
                provider_details['safety_ratings'] = [
                    r.model_dump(by_alias=True) for r in response.prompt_feedback.safety_ratings
                ]
            finish_reason = 'content_filter'

        if response.create_time is not None:  # pragma: no branch
            provider_details['timestamp'] = response.create_time

        if (
            response.sdk_http_response
            and response.sdk_http_response.headers
            and (service_tier := response.sdk_http_response.headers.get('x-gemini-service-tier'))
        ):
            provider_details['service_tier'] = service_tier.lower()

        # Add traffic_type to provider_details for Flex PayGo verification
        if response.usage_metadata and response.usage_metadata.traffic_type:
            provider_details['traffic_type'] = response.usage_metadata.traffic_type.value

        if candidate is None or candidate.content is None or candidate.content.parts is None:
            parts = []
        else:
            parts = candidate.content.parts or []

        if candidate and (logprob_results := candidate.logprobs_result):
            provider_details['logprobs'] = logprob_results.model_dump(mode='json')
            provider_details['avg_logprobs'] = candidate.avg_logprobs

        usage = _metadata_as_usage(response, provider=self._provider.name, provider_url=self._provider.base_url)
        grounding_metadata = candidate.grounding_metadata if candidate else None
        url_context_metadata = candidate.url_context_metadata if candidate else None

        return _process_response_from_parts(
            parts,
            grounding_metadata,
            response.model_version or self._model_name,
            self._provider.name,
            self._provider.base_url,
            usage,
            provider_response_id=provider_response_id,
            provider_details=provider_details or None,
            finish_reason=finish_reason,
            url_context_metadata=url_context_metadata,
        )

    async def _process_streamed_response(
        self, response: AsyncIterator[GenerateContentResponse], model_request_parameters: ModelRequestParameters
    ) -> StreamedResponse:
        """Process a streamed response, and prepare a streaming response to return."""
        peekable_response: _utils.PeekableAsyncStream[
            GenerateContentResponse, AsyncIterator[GenerateContentResponse]
        ] = _utils.PeekableAsyncStream(response)
        first_chunk = await peekable_response.peek()
        if isinstance(first_chunk, _utils.Unset):
            raise UnexpectedModelBehavior('Streamed response ended without content or tool calls')  # pragma: no cover

        return GeminiStreamedResponse(
            model_request_parameters=model_request_parameters,
            _model_name=first_chunk.model_version or self._model_name,
            _response=peekable_response,
            _provider_name=self._provider.name,
            _provider_url=self._provider.base_url,
            _provider_timestamp=first_chunk.create_time,
        )

    async def _map_messages(  # noqa: C901
        self,
        messages: list[ModelMessage],
        model_request_parameters: ModelRequestParameters,
    ) -> tuple[ContentDict | None, list[ContentUnionDict]]:
        supports_tool_combination = self.profile.get('google_supports_tool_combination', False)
        contents: list[ContentUnionDict] = []
        system_parts: list[PartDict] = []

        for m in messages:
            if isinstance(m, ModelRequest):
                message_parts: list[PartDict] = []

                for part in m.parts:
                    if isinstance(part, SystemPromptPart):
                        system_parts.append({'text': part.content})
                    elif isinstance(part, UserPromptPart):
                        message_parts.extend(await self._map_user_prompt(part))
                    elif isinstance(part, ToolReturnPart):
                        message_parts.extend(await self._map_tool_return(part))
                    elif isinstance(part, RetryPromptPart):
                        if part.tool_name is None:
                            message_parts.append({'text': part.model_response()})
                        else:
                            message_parts.append(
                                {
                                    'function_response': {
                                        'name': part.tool_name,
                                        'response': {'error': part.model_response()},
                                        'id': part.tool_call_id,
                                    }
                                }
                            )
                    else:
                        assert_never(part)

                # Work around a Gemini bug where content objects containing functionResponse parts are treated as
                # role=model even when role=user is explicitly specified.
                #
                # We build `message_parts` first, then split into multiple content objects whenever we transition
                # between function_response and non-function_response parts.
                #
                # TODO: Remove workaround when https://github.com/pydantic/pydantic-ai/issues/4210 is resolved
                if message_parts:
                    content_parts: list[PartDict] = []

                    for part in message_parts:
                        if (
                            content_parts
                            and 'function_response' in content_parts[-1]
                            and 'function_response' not in part
                        ):
                            contents.append({'role': 'user', 'parts': content_parts})
                            content_parts = []

                        content_parts.append(part)

                    contents.append({'role': 'user', 'parts': content_parts})
            elif isinstance(m, ModelResponse):
                maybe_content = _content_model_response(
                    m, self._matching_provider_names, supports_tool_combination=supports_tool_combination
                )
                if maybe_content:
                    contents.append(maybe_content)
            else:
                assert_never(m)

        # Google GenAI requires at least one user part in the message, and that function call turns
        # come immediately after a user turn or after a function response turn.
        if not contents or contents[0].get('role') == 'model':  # pyright: ignore[reportAttributeAccessIssue, reportUnknownMemberType]
            contents.insert(0, {'role': 'user', 'parts': [{'text': ''}]})

        if instruction_parts := self._get_instruction_parts(messages, model_request_parameters):
            for part in instruction_parts:
                system_parts.append({'text': part.content})
        system_instruction = ContentDict(role='user', parts=system_parts) if system_parts else None

        return system_instruction, contents

    async def _map_tool_return(self, part: ToolReturnPart) -> list[PartDict]:
        """Map a `ToolReturnPart` to Google API format, handling multimodal content.

        For Gemini 3+ models with supported MIME types, files are sent inside
        `function_response.parts` for efficiency. Unsupported types become separate
        parts after the function_response (fallback strategy).
        See: https://ai.google.dev/gemini-api/docs/function-calling?example=meeting#multimodal
        """
        supported_mime_types = self.profile.get('google_supported_mime_types_in_tool_returns', ())

        function_response_parts: list[FunctionResponsePartDict] = []
        fallback_parts: list[PartDict] = []
        fallback_refs: list[str] = []

        for file in part.files:
            if file.media_type in supported_mime_types:
                fr_part = await self._map_file_to_function_response_part(file)
                function_response_parts.append(fr_part)
            else:
                fallback_refs.append(f'See file {file.identifier}.')
                fallback_parts.append({'text': f'This is file {file.identifier}:'})
                file_part = await self._map_file_to_part(file)
                fallback_parts.append(file_part)

        response = part.model_response_object()
        if fallback_refs:
            response = {'output': [response, *fallback_refs]}

        function_response_dict: FunctionResponseDict = {
            'name': part.tool_name,
            'response': response,
            'id': part.tool_call_id,
        }
        if function_response_parts:
            function_response_dict['parts'] = function_response_parts

        result: list[PartDict] = [{'function_response': function_response_dict}]
        result.extend(fallback_parts)

        return result

    def _validate_uploaded_file(self, file: UploadedFile) -> tuple[str, str]:
        """Validate an `UploadedFile` and return (`file_uri`, `mime_type`).

        The Gemini API uses the Files API (https:// URIs). Google Cloud uses GCS
        (gs:// URIs). The Files API is not available on Google Cloud.
        """
        if file.provider_name not in self._matching_provider_names:
            raise UserError(
                f'UploadedFile with `provider_name={file.provider_name!r}` cannot be used with GoogleModel. '
                f'Expected `provider_name` to be one of {sorted(self._matching_provider_names)!r}.'
            )
        if self.system in _GOOGLE_CLOUD_PROVIDER_NAMES:
            if not file.file_id.startswith('gs://'):
                raise UserError(
                    f'UploadedFile for GoogleModel (Google Cloud) must use a GCS URI (gs://bucket/path), got: {file.file_id}'
                )
        elif not file.file_id.startswith('https://'):
            raise UserError(
                f'UploadedFile for GoogleModel (Gemini API) must use a file URI from the Google Files API '
                f'(https://generativelanguage.googleapis.com/...), got: {file.file_id}'
            )
        return file.file_id, file.media_type

    async def _resolve_file(
        self, file: FileUrl | BinaryContent | UploadedFile
    ) -> tuple[Literal['inline'], bytes, str] | tuple[Literal['file'], str, str]:
        """Resolve a file to either inline data `('inline', data, mime_type)` or a file reference `('file', uri, mime_type)`.

        Shared resolution logic for both `_map_file_to_part` and `_map_file_to_function_response_part`.
        """
        if isinstance(file, BinaryContent):
            return ('inline', file.data, file.media_type)
        elif isinstance(file, UploadedFile):
            file_uri, mime_type = self._validate_uploaded_file(file)
            return ('file', file_uri, mime_type)
        elif isinstance(file, VideoUrl) and (
            file.is_youtube or (file.url.startswith('gs://') and self.system in _GOOGLE_CLOUD_PROVIDER_NAMES)
        ):
            return ('file', file.url, file.media_type)
        elif isinstance(file, FileUrl):
            if file.force_download or (
                self.system in _GEMINI_API_PROVIDER_NAMES
                and not file.url.startswith(r'https://generativelanguage.googleapis.com/v1beta/files')
            ):
                downloaded_item = await download_item(file, data_format='bytes')
                return ('inline', downloaded_item['data'], downloaded_item['data_type'])
            else:
                return ('file', file.url, file.media_type)  # pragma: lax no cover
        else:
            assert_never(file)

    async def _map_file_to_part(self, file: FileUrl | BinaryContent | UploadedFile) -> PartDict:
        """Map a multimodal file directly to a Google API `PartDict`."""
        resolved = await self._resolve_file(file)
        part_dict: PartDict
        if resolved[0] == 'inline':
            part_dict = {'inline_data': BlobDict(data=resolved[1], mime_type=resolved[2])}
        else:
            part_dict = {'file_data': FileDataDict(file_uri=resolved[1], mime_type=resolved[2])}
        if isinstance(file, (BinaryContent, VideoUrl, UploadedFile)) and file.vendor_metadata:
            part_dict['video_metadata'] = cast(VideoMetadataDict, file.vendor_metadata)
        return part_dict

    async def _map_file_to_function_response_part(
        self, file: FileUrl | BinaryContent | UploadedFile
    ) -> FunctionResponsePartDict:
        """Map a multimodal file to `FunctionResponsePartDict` for Gemini 3+ native tool returns.

        Note: `FunctionResponseBlobDict`/`FunctionResponseFileDataDict` declare `display_name` but
        the google-genai SDK's `_live_converters.py` rejects it at runtime. We omit it until the
        SDK supports it, at which point we could also add `$ref` identifiers in the response dict.
        """
        resolved = await self._resolve_file(file)
        if resolved[0] == 'inline':
            blob_dict: FunctionResponseBlobDict = {'data': resolved[1], 'mime_type': resolved[2]}
            return FunctionResponsePartDict(inline_data=blob_dict)
        else:
            file_data_dict: FunctionResponseFileDataDict = {'file_uri': resolved[1], 'mime_type': resolved[2]}
            return FunctionResponsePartDict(file_data=file_data_dict)

    async def _map_user_prompt(self, part: UserPromptPart) -> list[PartDict]:
        if isinstance(part.content, str):
            return [{'text': part.content}]
        else:
            content: list[PartDict] = []
            for item in part.content:
                if isinstance(item, str | TextContent):
                    text = item if isinstance(item, str) else item.content
                    content.append({'text': text})
                elif isinstance(item, (BinaryContent, FileUrl, UploadedFile)):
                    file_part = await self._map_file_to_part(item)
                    content.append(file_part)
                elif isinstance(item, CachePoint):
                    # Google doesn't support inline CachePoint markers. Google's caching requires
                    # pre-creating cache objects via the API, then referencing them by name using
                    # `GoogleModelSettings.google_cached_content`. See https://ai.google.dev/gemini-api/docs/caching
                    pass
                else:
                    assert_never(item)
        return content

    def _map_response_schema(self, o: OutputObjectDefinition) -> dict[str, Any]:
        response_schema = o.json_schema.copy()
        if o.name:
            response_schema['title'] = o.name
        if o.description:
            response_schema['description'] = o.description

        return response_schema

__init__

__init__(
    model_name: GoogleModelName,
    *,
    provider: (
        Literal["google", "google-cloud", "gateway"]
        | Provider[Client]
    ) = "google",
    profile: ModelProfileSpec | None = None,
    settings: ModelSettings | None = None
)

Initialize a Gemini model.

Parameters:

Name Type Description Default
model_name GoogleModelName

The name of the model to use.

required
provider Literal['google', 'google-cloud', 'gateway'] | Provider[Client]

The provider to use for authentication and API access. Can be either the string 'google' (Gemini API) or 'google-cloud' (Google Cloud, formerly known as Vertex AI), or an instance of Provider[google.genai.AsyncClient]. Defaults to 'google'.

'google'
profile ModelProfileSpec | None

The model profile to use. Defaults to a profile picked by the provider based on the model name.

None
settings ModelSettings | None

The model settings to use. Defaults to None.

None
Source code in pydantic_ai_slim/pydantic_ai/models/google.py
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
def __init__(
    self,
    model_name: GoogleModelName,
    *,
    provider: Literal['google', 'google-cloud', 'gateway'] | Provider[Client] = 'google',
    profile: ModelProfileSpec | None = None,
    settings: ModelSettings | None = None,
):
    """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' (Gemini API) or 'google-cloud' (Google Cloud, formerly known as Vertex AI),
            or an instance of `Provider[google.genai.AsyncClient]`. Defaults to 'google'.
        profile: The model profile to use. Defaults to a profile picked by the provider based on the model name.
        settings: The model settings to use. Defaults to None.
    """
    self._model_name = model_name

    if isinstance(provider, str):
        provider = infer_provider('gateway/google-cloud' if provider == 'gateway' else provider)
    self._provider = provider

    super().__init__(settings=settings, profile=profile)

model_name property

model_name: GoogleModelName

The model name.

system property

system: str

The model provider.

supported_native_tools classmethod

supported_native_tools() -> (
    frozenset[type[AbstractNativeTool]]
)

Return the set of native tool types this model can handle.

Source code in pydantic_ai_slim/pydantic_ai/models/google.py
423
424
425
426
@classmethod
def supported_native_tools(cls) -> frozenset[type[AbstractNativeTool]]:
    """Return the set of native tool types this model can handle."""
    return frozenset({WebSearchTool, CodeExecutionTool, FileSearchTool, WebFetchTool, ImageGenerationTool})

GeminiStreamedResponse dataclass

Bases: StreamedResponse

Implementation of StreamedResponse for the Gemini model.

Source code in pydantic_ai_slim/pydantic_ai/models/google.py
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
@dataclass
class GeminiStreamedResponse(StreamedResponse):
    """Implementation of `StreamedResponse` for the Gemini model."""

    _model_name: GoogleModelName
    _response: _utils.PeekableAsyncStream[GenerateContentResponse, AsyncIterator[GenerateContentResponse]]
    _provider_name: str
    _provider_url: str
    _provider_timestamp: datetime | None = None
    _timestamp: datetime = field(default_factory=_utils.now_utc)
    _file_search_tool_call_id: str | None = field(default=None, init=False)
    _code_execution_tool_call_id: str | None = field(default=None, init=False)
    _has_content_filter: bool = field(default=False, init=False)
    _has_tool_invocations: bool = field(default=False, init=False)

    async def close_stream(self) -> None:
        try:
            # google.genai types this as AsyncIterator, but at runtime it's an
            # async generator that exposes aclose().
            await self._response.source.aclose()  # pyright: ignore[reportAttributeAccessIssue, reportUnknownMemberType]
        except RuntimeError as exc:
            if not _utils.is_async_generator_already_running(exc):
                raise

    async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:  # noqa: C901
        if self._provider_timestamp is not None:
            self.provider_details = {'timestamp': self._provider_timestamp}
        try:
            async for chunk in self._response:
                self._usage = _metadata_as_usage(chunk, self._provider_name, self._provider_url)

                if (
                    chunk.sdk_http_response
                    and chunk.sdk_http_response.headers
                    and (service_tier := chunk.sdk_http_response.headers.get('x-gemini-service-tier'))
                ):
                    self.provider_details = {**(self.provider_details or {}), 'service_tier': service_tier.lower()}

                # Capture traffic_type before the candidates guard, since usage_metadata
                # may be present on chunks without candidates.
                if chunk.usage_metadata and chunk.usage_metadata.traffic_type:
                    self.provider_details = {
                        **(self.provider_details or {}),
                        'traffic_type': chunk.usage_metadata.traffic_type.value,
                    }

                if not chunk.candidates:
                    if chunk.prompt_feedback and chunk.prompt_feedback.block_reason:
                        self._has_content_filter = True
                        block_reason = chunk.prompt_feedback.block_reason
                        self.provider_details = {
                            **(self.provider_details or {}),
                            'block_reason': block_reason.value,
                        }
                        if chunk.prompt_feedback.block_reason_message:
                            self.provider_details['block_reason_message'] = chunk.prompt_feedback.block_reason_message
                        if chunk.prompt_feedback.safety_ratings:
                            self.provider_details['safety_ratings'] = [
                                r.model_dump(by_alias=True) for r in chunk.prompt_feedback.safety_ratings
                            ]
                        self.finish_reason = 'content_filter'
                        if chunk.response_id:  # pragma: no branch
                            self.provider_response_id = chunk.response_id
                    continue

                candidate = chunk.candidates[0]

                if chunk.response_id:  # pragma: no branch
                    self.provider_response_id = chunk.response_id

                raw_finish_reason = candidate.finish_reason
                if raw_finish_reason and not self._has_content_filter:
                    self.provider_details = {
                        **(self.provider_details or {}),
                        'finish_reason': raw_finish_reason.value,
                    }

                    if candidate.safety_ratings:
                        self.provider_details['safety_ratings'] = [
                            r.model_dump(by_alias=True) for r in candidate.safety_ratings
                        ]

                    self.finish_reason = _FINISH_REASON_MAP.get(raw_finish_reason)

                # Google streams the grounding metadata (including the web search queries and results)
                # _after_ the text that was generated using it, so it would show up out of order in the stream,
                # and cause issues with the logic that doesn't consider text ahead of built-in tool calls as output.
                # If that gets fixed (or we have a workaround), we can uncomment this:
                # web_search_call, web_search_return = _map_grounding_metadata(
                #     candidate.grounding_metadata, self.provider_name
                # )
                # if web_search_call and web_search_return:
                #     yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=web_search_call)
                #     yield self._parts_manager.handle_part(
                #         vendor_part_id=uuid4(), part=web_search_return
                #     )

                # URL context metadata (for WebFetchTool) is streamed in the first chunk, before the text,
                # so we can safely yield it here.
                #
                # `_has_tool_invocations` reflects parts seen in *prior* chunks because we can't peek
                # ahead in a stream. The non-streaming path (`_has_native_tool_invocations(parts)` in
                # `_process_response_from_parts`) inspects all parts upfront and is safer. The
                # streaming assumption — confirmed by Gemini 3 cassettes — is that
                # `url_context_metadata` and native `tool_call`/`tool_response` parts are mutually
                # exclusive: when `include_server_side_tool_invocations=True` the API returns
                # tool_call/tool_response parts *instead of* the metadata, never both.
                if not self._has_tool_invocations:
                    web_fetch_call, web_fetch_return = _map_url_context_metadata(
                        candidate.url_context_metadata, self.provider_name
                    )
                    if web_fetch_call and web_fetch_return:
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=web_fetch_call)
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=web_fetch_return)

                if candidate.content is None or candidate.content.parts is None:
                    continue

                parts = candidate.content.parts
                if not parts:
                    continue  # pragma: no cover

                if not self._has_tool_invocations:
                    self._has_tool_invocations = _has_native_tool_invocations(parts)

                for part in parts:
                    provider_details: dict[str, Any] | None = None
                    if part.thought_signature:
                        # Per https://ai.google.dev/gemini-api/docs/function-calling?example=meeting#thought-signatures:
                        # - Always send the thought_signature back to the model inside its original Part.
                        # - Don't merge a Part containing a signature with one that does not. This breaks the positional context of the thought.
                        # - Don't combine two Parts that both contain signatures, as the signature strings cannot be merged.
                        thought_signature = base64.b64encode(part.thought_signature).decode('utf-8')
                        provider_details = {'thought_signature': thought_signature}

                    if part.text is not None:
                        if len(part.text) == 0 and not provider_details:
                            continue
                        if part.thought:
                            for event in self._parts_manager.handle_thinking_delta(
                                vendor_part_id=None,
                                content=part.text,
                                provider_name=self.provider_name if provider_details else None,
                                provider_details=provider_details,
                            ):
                                yield event
                        else:
                            for event in self._parts_manager.handle_text_delta(
                                vendor_part_id=None,
                                content=part.text,
                                provider_name=self.provider_name if provider_details else None,
                                provider_details=provider_details,
                            ):
                                yield event
                    elif part.function_call:
                        maybe_event = self._parts_manager.handle_tool_call_delta(
                            vendor_part_id=uuid4(),
                            tool_name=part.function_call.name,
                            args=part.function_call.args,
                            tool_call_id=part.function_call.id,
                            provider_name=self.provider_name if provider_details else None,
                            provider_details=provider_details,
                        )
                        if maybe_event is not None:  # pragma: no branch
                            yield maybe_event
                    elif part.inline_data is not None:
                        if part.thought:  # pragma: no cover
                            # Per https://ai.google.dev/gemini-api/docs/image-generation#thinking-process:
                            # > The model generates up to two interim images to test composition and logic. The last image within Thinking is also the final rendered image.
                            # We currently don't expose these image thoughts as they can't be represented with `ThinkingPart`
                            continue
                        data = part.inline_data.data
                        mime_type = part.inline_data.mime_type
                        assert data and mime_type, 'Inline data must have data and mime type'
                        content = BinaryContent(data=data, media_type=mime_type)
                        yield self._parts_manager.handle_part(
                            vendor_part_id=uuid4(),
                            part=FilePart(
                                content=BinaryContent.narrow_type(content),
                                provider_name=self.provider_name if provider_details else None,
                                provider_details=provider_details,
                            ),
                        )
                    elif part.tool_call:
                        tool_call_part = _map_tool_call(part.tool_call, self.provider_name)
                        tool_call_part.provider_details = provider_details
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=tool_call_part)
                    elif part.tool_response:
                        tool_response_part = _map_tool_response(part.tool_response, self.provider_name)
                        tool_response_part.provider_details = provider_details
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=tool_response_part)
                    elif part.executable_code is not None:
                        part_obj = self._handle_executable_code_streaming(part.executable_code)
                        part_obj.provider_details = provider_details
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=part_obj)
                    elif part.code_execution_result is not None:
                        part = self._map_code_execution_result(part.code_execution_result)
                        part.provider_details = provider_details
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=part)
                    else:
                        assert part.function_response is not None, f'Unexpected part: {part}'  # pragma: no cover

                # Grounding metadata is attached to the final text chunk, so
                # we emit the `NativeToolReturnPart` after the text delta so
                # that the delta is properly added to the same `TextPart` as earlier chunks
                if not self._has_tool_invocations:
                    file_search_part = self._handle_file_search_grounding_metadata_streaming(
                        candidate.grounding_metadata
                    )
                    if file_search_part is not None:
                        yield self._parts_manager.handle_part(vendor_part_id=uuid4(), part=file_search_part)
        except errors.APIError as e:
            if (status_code := e.code) >= 400:
                raise ModelHTTPError(
                    status_code=status_code,
                    model_name=self._model_name,
                    body=cast(Any, e.details),  # pyright: ignore[reportUnknownMemberType]
                ) from e
            raise ModelAPIError(model_name=self._model_name, message=str(e)) from e

    def _handle_file_search_grounding_metadata_streaming(
        self, grounding_metadata: GroundingMetadata | None
    ) -> NativeToolReturnPart | None:
        """Handle file search grounding metadata for streaming responses.

        Returns a NativeToolReturnPart if file search results are available in the grounding metadata.
        """
        if not self._file_search_tool_call_id or not grounding_metadata:
            return None

        grounding_chunks = grounding_metadata.grounding_chunks
        retrieved_contexts = _extract_file_search_retrieved_contexts(grounding_chunks)
        if retrieved_contexts:  # pragma: no branch
            part = NativeToolReturnPart(
                provider_name=self.provider_name,
                tool_name=FileSearchTool.kind,
                tool_call_id=self._file_search_tool_call_id,
                content=retrieved_contexts,
            )
            self._file_search_tool_call_id = None
            return part
        return None  # pragma: no cover

    def _map_code_execution_result(self, code_execution_result: CodeExecutionResult) -> NativeToolReturnPart:
        """Map code execution result to a NativeToolReturnPart using instance state."""
        assert self._code_execution_tool_call_id is not None
        return _map_code_execution_result(code_execution_result, self.provider_name, self._code_execution_tool_call_id)

    def _handle_executable_code_streaming(self, executable_code: ExecutableCode) -> ModelResponsePart:
        """Handle executable code for streaming responses.

        Returns a NativeToolCallPart for file search or code execution.
        Sets self._code_execution_tool_call_id or self._file_search_tool_call_id as appropriate.
        """
        code = executable_code.code
        has_file_search_tool = any(
            isinstance(tool, FileSearchTool) for tool in self.model_request_parameters.native_tools
        )

        if code and has_file_search_tool and (file_search_query := self._extract_file_search_query(code)):
            self._file_search_tool_call_id = _utils.generate_tool_call_id()
            return NativeToolCallPart(
                provider_name=self.provider_name,
                tool_name=FileSearchTool.kind,
                tool_call_id=self._file_search_tool_call_id,
                args={'query': file_search_query},
            )

        self._code_execution_tool_call_id = _utils.generate_tool_call_id()
        return _map_executable_code(executable_code, self.provider_name, self._code_execution_tool_call_id)

    def _extract_file_search_query(self, code: str) -> str | None:
        """Extract the query from file_search.query() executable code.

        Handles escaped quotes in the query string.

        Example: 'print(file_search.query(query="what is the capital of France?"))'
        Returns: 'what is the capital of France?'
        """
        match = _FILE_SEARCH_QUERY_PATTERN.search(code)
        if match:
            query = match.group(2)
            query = query.replace('\\\\', '\\').replace('\\"', '"').replace("\\'", "'")
            return query
        return None  # pragma: no cover

    @property
    def model_name(self) -> GoogleModelName:
        """Get the model name of the response."""
        return self._model_name

    @property
    def provider_name(self) -> str:
        """Get the provider name."""
        return self._provider_name

    @property
    def provider_url(self) -> str:
        """Get the provider base URL."""
        return self._provider_url

    @property
    def timestamp(self) -> datetime:
        """Get the timestamp of the response."""
        return self._timestamp

model_name property

model_name: GoogleModelName

Get the model name of the response.

provider_name property

provider_name: str

Get the provider name.

provider_url property

provider_url: str

Get the provider base URL.

timestamp property

timestamp: datetime

Get the timestamp of the response.