google-genai 1.54.0__py3-none-any.whl → 1.55.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. google/genai/__init__.py +1 -0
  2. google/genai/_interactions/__init__.py +117 -0
  3. google/genai/_interactions/_base_client.py +2019 -0
  4. google/genai/_interactions/_client.py +511 -0
  5. google/genai/_interactions/_compat.py +234 -0
  6. google/genai/_interactions/_constants.py +29 -0
  7. google/genai/_interactions/_exceptions.py +122 -0
  8. google/genai/_interactions/_files.py +139 -0
  9. google/genai/_interactions/_models.py +873 -0
  10. google/genai/_interactions/_qs.py +165 -0
  11. google/genai/_interactions/_resource.py +58 -0
  12. google/genai/_interactions/_response.py +847 -0
  13. google/genai/_interactions/_streaming.py +354 -0
  14. google/genai/_interactions/_types.py +276 -0
  15. google/genai/_interactions/_utils/__init__.py +79 -0
  16. google/genai/_interactions/_utils/_compat.py +61 -0
  17. google/genai/_interactions/_utils/_datetime_parse.py +151 -0
  18. google/genai/_interactions/_utils/_logs.py +40 -0
  19. google/genai/_interactions/_utils/_proxy.py +80 -0
  20. google/genai/_interactions/_utils/_reflection.py +57 -0
  21. google/genai/_interactions/_utils/_resources_proxy.py +39 -0
  22. google/genai/_interactions/_utils/_streams.py +27 -0
  23. google/genai/_interactions/_utils/_sync.py +73 -0
  24. google/genai/_interactions/_utils/_transform.py +472 -0
  25. google/genai/_interactions/_utils/_typing.py +172 -0
  26. google/genai/_interactions/_utils/_utils.py +437 -0
  27. google/genai/_interactions/_version.py +18 -0
  28. google/genai/_interactions/resources/__init__.py +34 -0
  29. google/genai/_interactions/resources/interactions.py +1350 -0
  30. google/genai/_interactions/types/__init__.py +107 -0
  31. google/genai/_interactions/types/allowed_tools.py +33 -0
  32. google/genai/_interactions/types/allowed_tools_param.py +35 -0
  33. google/genai/_interactions/types/annotation.py +42 -0
  34. google/genai/_interactions/types/annotation_param.py +42 -0
  35. google/genai/_interactions/types/audio_content.py +38 -0
  36. google/genai/_interactions/types/audio_content_param.py +45 -0
  37. google/genai/_interactions/types/audio_mime_type.py +25 -0
  38. google/genai/_interactions/types/audio_mime_type_param.py +27 -0
  39. google/genai/_interactions/types/code_execution_call_arguments.py +33 -0
  40. google/genai/_interactions/types/code_execution_call_arguments_param.py +32 -0
  41. google/genai/_interactions/types/code_execution_call_content.py +37 -0
  42. google/genai/_interactions/types/code_execution_call_content_param.py +37 -0
  43. google/genai/_interactions/types/code_execution_result_content.py +42 -0
  44. google/genai/_interactions/types/code_execution_result_content_param.py +41 -0
  45. google/genai/_interactions/types/content_delta.py +358 -0
  46. google/genai/_interactions/types/content_start.py +79 -0
  47. google/genai/_interactions/types/content_stop.py +35 -0
  48. google/genai/_interactions/types/deep_research_agent_config.py +33 -0
  49. google/genai/_interactions/types/deep_research_agent_config_param.py +32 -0
  50. google/genai/_interactions/types/document_content.py +36 -0
  51. google/genai/_interactions/types/document_content_param.py +43 -0
  52. google/genai/_interactions/types/dynamic_agent_config.py +44 -0
  53. google/genai/_interactions/types/dynamic_agent_config_param.py +33 -0
  54. google/genai/_interactions/types/error_event.py +46 -0
  55. google/genai/_interactions/types/file_search_result_content.py +46 -0
  56. google/genai/_interactions/types/file_search_result_content_param.py +46 -0
  57. google/genai/_interactions/types/function.py +38 -0
  58. google/genai/_interactions/types/function_call_content.py +39 -0
  59. google/genai/_interactions/types/function_call_content_param.py +39 -0
  60. google/genai/_interactions/types/function_param.py +37 -0
  61. google/genai/_interactions/types/function_result_content.py +52 -0
  62. google/genai/_interactions/types/function_result_content_param.py +54 -0
  63. google/genai/_interactions/types/generation_config.py +57 -0
  64. google/genai/_interactions/types/generation_config_param.py +59 -0
  65. google/genai/_interactions/types/google_search_call_arguments.py +29 -0
  66. google/genai/_interactions/types/google_search_call_arguments_param.py +31 -0
  67. google/genai/_interactions/types/google_search_call_content.py +37 -0
  68. google/genai/_interactions/types/google_search_call_content_param.py +37 -0
  69. google/genai/_interactions/types/google_search_result.py +35 -0
  70. google/genai/_interactions/types/google_search_result_content.py +43 -0
  71. google/genai/_interactions/types/google_search_result_content_param.py +44 -0
  72. google/genai/_interactions/types/google_search_result_param.py +35 -0
  73. google/genai/_interactions/types/image_content.py +41 -0
  74. google/genai/_interactions/types/image_content_param.py +48 -0
  75. google/genai/_interactions/types/image_mime_type.py +23 -0
  76. google/genai/_interactions/types/image_mime_type_param.py +25 -0
  77. google/genai/_interactions/types/interaction.py +165 -0
  78. google/genai/_interactions/types/interaction_create_params.py +212 -0
  79. google/genai/_interactions/types/interaction_event.py +37 -0
  80. google/genai/_interactions/types/interaction_get_params.py +46 -0
  81. google/genai/_interactions/types/interaction_sse_event.py +32 -0
  82. google/genai/_interactions/types/interaction_status_update.py +37 -0
  83. google/genai/_interactions/types/mcp_server_tool_call_content.py +42 -0
  84. google/genai/_interactions/types/mcp_server_tool_call_content_param.py +42 -0
  85. google/genai/_interactions/types/mcp_server_tool_result_content.py +52 -0
  86. google/genai/_interactions/types/mcp_server_tool_result_content_param.py +54 -0
  87. google/genai/_interactions/types/model.py +36 -0
  88. google/genai/_interactions/types/model_param.py +38 -0
  89. google/genai/_interactions/types/speech_config.py +35 -0
  90. google/genai/_interactions/types/speech_config_param.py +35 -0
  91. google/genai/_interactions/types/text_content.py +37 -0
  92. google/genai/_interactions/types/text_content_param.py +38 -0
  93. google/genai/_interactions/types/thinking_level.py +22 -0
  94. google/genai/_interactions/types/thought_content.py +41 -0
  95. google/genai/_interactions/types/thought_content_param.py +47 -0
  96. google/genai/_interactions/types/tool.py +100 -0
  97. google/genai/_interactions/types/tool_choice.py +26 -0
  98. google/genai/_interactions/types/tool_choice_config.py +28 -0
  99. google/genai/_interactions/types/tool_choice_config_param.py +29 -0
  100. google/genai/_interactions/types/tool_choice_param.py +28 -0
  101. google/genai/_interactions/types/tool_choice_type.py +22 -0
  102. google/genai/_interactions/types/tool_param.py +97 -0
  103. google/genai/_interactions/types/turn.py +76 -0
  104. google/genai/_interactions/types/turn_param.py +73 -0
  105. google/genai/_interactions/types/url_context_call_arguments.py +29 -0
  106. google/genai/_interactions/types/url_context_call_arguments_param.py +31 -0
  107. google/genai/_interactions/types/url_context_call_content.py +37 -0
  108. google/genai/_interactions/types/url_context_call_content_param.py +37 -0
  109. google/genai/_interactions/types/url_context_result.py +33 -0
  110. google/genai/_interactions/types/url_context_result_content.py +43 -0
  111. google/genai/_interactions/types/url_context_result_content_param.py +44 -0
  112. google/genai/_interactions/types/url_context_result_param.py +32 -0
  113. google/genai/_interactions/types/usage.py +106 -0
  114. google/genai/_interactions/types/usage_param.py +106 -0
  115. google/genai/_interactions/types/video_content.py +41 -0
  116. google/genai/_interactions/types/video_content_param.py +48 -0
  117. google/genai/_interactions/types/video_mime_type.py +36 -0
  118. google/genai/_interactions/types/video_mime_type_param.py +38 -0
  119. google/genai/_live_converters.py +31 -0
  120. google/genai/_tokens_converters.py +5 -0
  121. google/genai/batches.py +7 -0
  122. google/genai/client.py +223 -0
  123. google/genai/interactions.py +17 -0
  124. google/genai/live.py +4 -3
  125. google/genai/models.py +12 -0
  126. google/genai/tests/__init__.py +21 -0
  127. google/genai/tests/afc/__init__.py +21 -0
  128. google/genai/tests/afc/test_convert_if_exist_pydantic_model.py +309 -0
  129. google/genai/tests/afc/test_convert_number_values_for_function_call_args.py +63 -0
  130. google/genai/tests/afc/test_find_afc_incompatible_tool_indexes.py +240 -0
  131. google/genai/tests/afc/test_generate_content_stream_afc.py +530 -0
  132. google/genai/tests/afc/test_generate_content_stream_afc_thoughts.py +77 -0
  133. google/genai/tests/afc/test_get_function_map.py +176 -0
  134. google/genai/tests/afc/test_get_function_response_parts.py +277 -0
  135. google/genai/tests/afc/test_get_max_remote_calls_for_afc.py +130 -0
  136. google/genai/tests/afc/test_invoke_function_from_dict_args.py +241 -0
  137. google/genai/tests/afc/test_raise_error_for_afc_incompatible_config.py +159 -0
  138. google/genai/tests/afc/test_should_append_afc_history.py +53 -0
  139. google/genai/tests/afc/test_should_disable_afc.py +214 -0
  140. google/genai/tests/batches/__init__.py +17 -0
  141. google/genai/tests/batches/test_cancel.py +77 -0
  142. google/genai/tests/batches/test_create.py +78 -0
  143. google/genai/tests/batches/test_create_with_bigquery.py +113 -0
  144. google/genai/tests/batches/test_create_with_file.py +82 -0
  145. google/genai/tests/batches/test_create_with_gcs.py +125 -0
  146. google/genai/tests/batches/test_create_with_inlined_requests.py +255 -0
  147. google/genai/tests/batches/test_delete.py +86 -0
  148. google/genai/tests/batches/test_embedding.py +157 -0
  149. google/genai/tests/batches/test_get.py +78 -0
  150. google/genai/tests/batches/test_list.py +79 -0
  151. google/genai/tests/caches/__init__.py +17 -0
  152. google/genai/tests/caches/constants.py +29 -0
  153. google/genai/tests/caches/test_create.py +210 -0
  154. google/genai/tests/caches/test_create_custom_url.py +105 -0
  155. google/genai/tests/caches/test_delete.py +54 -0
  156. google/genai/tests/caches/test_delete_custom_url.py +52 -0
  157. google/genai/tests/caches/test_get.py +94 -0
  158. google/genai/tests/caches/test_get_custom_url.py +52 -0
  159. google/genai/tests/caches/test_list.py +68 -0
  160. google/genai/tests/caches/test_update.py +70 -0
  161. google/genai/tests/caches/test_update_custom_url.py +58 -0
  162. google/genai/tests/chats/__init__.py +1 -0
  163. google/genai/tests/chats/test_get_history.py +597 -0
  164. google/genai/tests/chats/test_send_message.py +844 -0
  165. google/genai/tests/chats/test_validate_response.py +90 -0
  166. google/genai/tests/client/__init__.py +17 -0
  167. google/genai/tests/client/test_async_stream.py +427 -0
  168. google/genai/tests/client/test_client_close.py +197 -0
  169. google/genai/tests/client/test_client_initialization.py +1687 -0
  170. google/genai/tests/client/test_client_requests.py +355 -0
  171. google/genai/tests/client/test_custom_client.py +77 -0
  172. google/genai/tests/client/test_http_options.py +178 -0
  173. google/genai/tests/client/test_replay_client_equality.py +168 -0
  174. google/genai/tests/client/test_retries.py +846 -0
  175. google/genai/tests/client/test_upload_errors.py +136 -0
  176. google/genai/tests/common/__init__.py +17 -0
  177. google/genai/tests/common/test_common.py +954 -0
  178. google/genai/tests/conftest.py +162 -0
  179. google/genai/tests/documents/__init__.py +17 -0
  180. google/genai/tests/documents/test_delete.py +51 -0
  181. google/genai/tests/documents/test_get.py +85 -0
  182. google/genai/tests/documents/test_list.py +72 -0
  183. google/genai/tests/errors/__init__.py +1 -0
  184. google/genai/tests/errors/test_api_error.py +417 -0
  185. google/genai/tests/file_search_stores/__init__.py +17 -0
  186. google/genai/tests/file_search_stores/test_create.py +66 -0
  187. google/genai/tests/file_search_stores/test_delete.py +64 -0
  188. google/genai/tests/file_search_stores/test_get.py +94 -0
  189. google/genai/tests/file_search_stores/test_import_file.py +112 -0
  190. google/genai/tests/file_search_stores/test_list.py +57 -0
  191. google/genai/tests/file_search_stores/test_upload_to_file_search_store.py +141 -0
  192. google/genai/tests/files/__init__.py +17 -0
  193. google/genai/tests/files/test_delete.py +46 -0
  194. google/genai/tests/files/test_download.py +85 -0
  195. google/genai/tests/files/test_get.py +46 -0
  196. google/genai/tests/files/test_list.py +72 -0
  197. google/genai/tests/files/test_upload.py +255 -0
  198. google/genai/tests/imports/test_no_optional_imports.py +28 -0
  199. google/genai/tests/interactions/__init__.py +0 -0
  200. google/genai/tests/interactions/test_integration.py +80 -0
  201. google/genai/tests/live/__init__.py +16 -0
  202. google/genai/tests/live/test_live.py +2177 -0
  203. google/genai/tests/live/test_live_music.py +362 -0
  204. google/genai/tests/live/test_live_response.py +163 -0
  205. google/genai/tests/live/test_send_client_content.py +147 -0
  206. google/genai/tests/live/test_send_realtime_input.py +268 -0
  207. google/genai/tests/live/test_send_tool_response.py +222 -0
  208. google/genai/tests/local_tokenizer/__init__.py +17 -0
  209. google/genai/tests/local_tokenizer/test_local_tokenizer.py +343 -0
  210. google/genai/tests/local_tokenizer/test_local_tokenizer_loader.py +235 -0
  211. google/genai/tests/mcp/__init__.py +17 -0
  212. google/genai/tests/mcp/test_has_mcp_tool_usage.py +89 -0
  213. google/genai/tests/mcp/test_mcp_to_gemini_tools.py +191 -0
  214. google/genai/tests/mcp/test_parse_config_for_mcp_sessions.py +201 -0
  215. google/genai/tests/mcp/test_parse_config_for_mcp_usage.py +130 -0
  216. google/genai/tests/mcp/test_set_mcp_usage_header.py +72 -0
  217. google/genai/tests/models/__init__.py +17 -0
  218. google/genai/tests/models/constants.py +8 -0
  219. google/genai/tests/models/test_compute_tokens.py +120 -0
  220. google/genai/tests/models/test_count_tokens.py +159 -0
  221. google/genai/tests/models/test_delete.py +107 -0
  222. google/genai/tests/models/test_edit_image.py +264 -0
  223. google/genai/tests/models/test_embed_content.py +94 -0
  224. google/genai/tests/models/test_function_call_streaming.py +442 -0
  225. google/genai/tests/models/test_generate_content.py +2502 -0
  226. google/genai/tests/models/test_generate_content_cached_content.py +132 -0
  227. google/genai/tests/models/test_generate_content_config_zero_value.py +103 -0
  228. google/genai/tests/models/test_generate_content_from_apikey.py +44 -0
  229. google/genai/tests/models/test_generate_content_http_options.py +40 -0
  230. google/genai/tests/models/test_generate_content_image_generation.py +143 -0
  231. google/genai/tests/models/test_generate_content_mcp.py +343 -0
  232. google/genai/tests/models/test_generate_content_media_resolution.py +97 -0
  233. google/genai/tests/models/test_generate_content_model.py +139 -0
  234. google/genai/tests/models/test_generate_content_part.py +821 -0
  235. google/genai/tests/models/test_generate_content_thought.py +76 -0
  236. google/genai/tests/models/test_generate_content_tools.py +1761 -0
  237. google/genai/tests/models/test_generate_images.py +191 -0
  238. google/genai/tests/models/test_generate_videos.py +759 -0
  239. google/genai/tests/models/test_get.py +104 -0
  240. google/genai/tests/models/test_list.py +233 -0
  241. google/genai/tests/models/test_recontext_image.py +189 -0
  242. google/genai/tests/models/test_segment_image.py +148 -0
  243. google/genai/tests/models/test_update.py +95 -0
  244. google/genai/tests/models/test_upscale_image.py +157 -0
  245. google/genai/tests/operations/__init__.py +17 -0
  246. google/genai/tests/operations/test_get.py +38 -0
  247. google/genai/tests/public_samples/__init__.py +17 -0
  248. google/genai/tests/public_samples/test_gemini_text_only.py +34 -0
  249. google/genai/tests/pytest_helper.py +229 -0
  250. google/genai/tests/shared/__init__.py +16 -0
  251. google/genai/tests/shared/batches/__init__.py +14 -0
  252. google/genai/tests/shared/batches/test_create_delete.py +57 -0
  253. google/genai/tests/shared/batches/test_create_get_cancel.py +56 -0
  254. google/genai/tests/shared/batches/test_list.py +40 -0
  255. google/genai/tests/shared/caches/__init__.py +14 -0
  256. google/genai/tests/shared/caches/test_create_get_delete.py +67 -0
  257. google/genai/tests/shared/caches/test_create_update_get.py +71 -0
  258. google/genai/tests/shared/caches/test_list.py +40 -0
  259. google/genai/tests/shared/chats/__init__.py +14 -0
  260. google/genai/tests/shared/chats/test_send_message.py +48 -0
  261. google/genai/tests/shared/chats/test_send_message_stream.py +50 -0
  262. google/genai/tests/shared/files/__init__.py +14 -0
  263. google/genai/tests/shared/files/test_list.py +41 -0
  264. google/genai/tests/shared/files/test_upload_get_delete.py +54 -0
  265. google/genai/tests/shared/models/__init__.py +14 -0
  266. google/genai/tests/shared/models/test_compute_tokens.py +41 -0
  267. google/genai/tests/shared/models/test_count_tokens.py +40 -0
  268. google/genai/tests/shared/models/test_edit_image.py +67 -0
  269. google/genai/tests/shared/models/test_embed.py +40 -0
  270. google/genai/tests/shared/models/test_generate_content.py +39 -0
  271. google/genai/tests/shared/models/test_generate_content_stream.py +54 -0
  272. google/genai/tests/shared/models/test_generate_images.py +40 -0
  273. google/genai/tests/shared/models/test_generate_videos.py +38 -0
  274. google/genai/tests/shared/models/test_list.py +37 -0
  275. google/genai/tests/shared/models/test_recontext_image.py +55 -0
  276. google/genai/tests/shared/models/test_segment_image.py +52 -0
  277. google/genai/tests/shared/models/test_upscale_image.py +52 -0
  278. google/genai/tests/shared/tunings/__init__.py +16 -0
  279. google/genai/tests/shared/tunings/test_create.py +46 -0
  280. google/genai/tests/shared/tunings/test_create_get_cancel.py +56 -0
  281. google/genai/tests/shared/tunings/test_list.py +39 -0
  282. google/genai/tests/tokens/__init__.py +16 -0
  283. google/genai/tests/tokens/test_create.py +154 -0
  284. google/genai/tests/transformers/__init__.py +17 -0
  285. google/genai/tests/transformers/test_blobs.py +71 -0
  286. google/genai/tests/transformers/test_bytes.py +15 -0
  287. google/genai/tests/transformers/test_duck_type.py +96 -0
  288. google/genai/tests/transformers/test_function_responses.py +72 -0
  289. google/genai/tests/transformers/test_schema.py +653 -0
  290. google/genai/tests/transformers/test_t_batch.py +286 -0
  291. google/genai/tests/transformers/test_t_content.py +160 -0
  292. google/genai/tests/transformers/test_t_contents.py +398 -0
  293. google/genai/tests/transformers/test_t_part.py +85 -0
  294. google/genai/tests/transformers/test_t_parts.py +87 -0
  295. google/genai/tests/transformers/test_t_tool.py +157 -0
  296. google/genai/tests/transformers/test_t_tools.py +195 -0
  297. google/genai/tests/tunings/__init__.py +16 -0
  298. google/genai/tests/tunings/test_cancel.py +39 -0
  299. google/genai/tests/tunings/test_end_to_end.py +106 -0
  300. google/genai/tests/tunings/test_get.py +67 -0
  301. google/genai/tests/tunings/test_list.py +75 -0
  302. google/genai/tests/tunings/test_tune.py +268 -0
  303. google/genai/tests/types/__init__.py +16 -0
  304. google/genai/tests/types/test_bytes_internal.py +271 -0
  305. google/genai/tests/types/test_bytes_type.py +152 -0
  306. google/genai/tests/types/test_future.py +101 -0
  307. google/genai/tests/types/test_optional_types.py +36 -0
  308. google/genai/tests/types/test_part_type.py +616 -0
  309. google/genai/tests/types/test_schema_from_json_schema.py +417 -0
  310. google/genai/tests/types/test_schema_json_schema.py +468 -0
  311. google/genai/tests/types/test_types.py +2903 -0
  312. google/genai/types.py +72 -0
  313. google/genai/version.py +1 -1
  314. {google_genai-1.54.0.dist-info → google_genai-1.55.0.dist-info}/METADATA +3 -1
  315. google_genai-1.55.0.dist-info/RECORD +345 -0
  316. google_genai-1.54.0.dist-info/RECORD +0 -41
  317. {google_genai-1.54.0.dist-info → google_genai-1.55.0.dist-info}/WHEEL +0 -0
  318. {google_genai-1.54.0.dist-info → google_genai-1.55.0.dist-info}/licenses/LICENSE +0 -0
  319. {google_genai-1.54.0.dist-info → google_genai-1.55.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,873 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+
16
+ # mypy: ignore-errors
17
+ from __future__ import annotations
18
+
19
+ import os
20
+ import inspect
21
+ import weakref
22
+ from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, Optional, cast
23
+ from datetime import date, datetime
24
+ from typing_extensions import (
25
+ List,
26
+ Unpack,
27
+ Literal,
28
+ ClassVar,
29
+ Protocol,
30
+ Required,
31
+ ParamSpec,
32
+ TypedDict,
33
+ TypeGuard,
34
+ final,
35
+ override,
36
+ runtime_checkable,
37
+ )
38
+
39
+ import pydantic
40
+ from pydantic.fields import FieldInfo
41
+
42
+ from ._types import (
43
+ Body,
44
+ IncEx,
45
+ Query,
46
+ ModelT,
47
+ Headers,
48
+ Timeout,
49
+ NotGiven,
50
+ AnyMapping,
51
+ HttpxRequestFiles,
52
+ )
53
+ from ._utils import (
54
+ PropertyInfo,
55
+ is_list,
56
+ is_given,
57
+ json_safe,
58
+ lru_cache,
59
+ is_mapping,
60
+ parse_date,
61
+ coerce_boolean,
62
+ parse_datetime,
63
+ strip_not_given,
64
+ extract_type_arg,
65
+ is_annotated_type,
66
+ is_type_alias_type,
67
+ strip_annotated_type,
68
+ )
69
+ from ._compat import (
70
+ PYDANTIC_V1,
71
+ ConfigDict,
72
+ GenericModel as BaseGenericModel,
73
+ get_args,
74
+ is_union,
75
+ parse_obj,
76
+ get_origin,
77
+ is_literal_type,
78
+ get_model_config,
79
+ get_model_fields,
80
+ field_get_default,
81
+ )
82
+ from ._constants import RAW_RESPONSE_HEADER
83
+
84
+ if TYPE_CHECKING:
85
+ from pydantic_core.core_schema import ModelField, ModelSchema, LiteralSchema, ModelFieldsSchema
86
+
87
+ __all__ = ["BaseModel", "GenericModel"]
88
+
89
+ _T = TypeVar("_T")
90
+ _BaseModelT = TypeVar("_BaseModelT", bound="BaseModel")
91
+
92
+ P = ParamSpec("P")
93
+
94
+
95
+ @runtime_checkable
96
+ class _ConfigProtocol(Protocol):
97
+ allow_population_by_field_name: bool
98
+
99
+
100
+ class BaseModel(pydantic.BaseModel):
101
+ if PYDANTIC_V1:
102
+
103
+ @property
104
+ @override
105
+ def model_fields_set(self) -> set[str]:
106
+ # a forwards-compat shim for pydantic v2
107
+ return self.__fields_set__ # type: ignore
108
+
109
+ class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
110
+ extra: Any = pydantic.Extra.allow # type: ignore
111
+ else:
112
+ model_config: ClassVar[ConfigDict] = ConfigDict(
113
+ extra="allow", defer_build=coerce_boolean(os.environ.get("DEFER_PYDANTIC_BUILD", "true"))
114
+ )
115
+
116
+ def to_dict(
117
+ self,
118
+ *,
119
+ mode: Literal["json", "python"] = "python",
120
+ use_api_names: bool = True,
121
+ exclude_unset: bool = True,
122
+ exclude_defaults: bool = False,
123
+ exclude_none: bool = False,
124
+ warnings: bool = True,
125
+ ) -> dict[str, object]:
126
+ """Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
127
+
128
+ By default, fields that were not set by the API will not be included,
129
+ and keys will match the API response, *not* the property names from the model.
130
+
131
+ For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
132
+ the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
133
+
134
+ Args:
135
+ mode:
136
+ If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`.
137
+ If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)`
138
+
139
+ use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
140
+ exclude_unset: Whether to exclude fields that have not been explicitly set.
141
+ exclude_defaults: Whether to exclude fields that are set to their default value from the output.
142
+ exclude_none: Whether to exclude fields that have a value of `None` from the output.
143
+ warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2.
144
+ """
145
+ return self.model_dump(
146
+ mode=mode,
147
+ by_alias=use_api_names,
148
+ exclude_unset=exclude_unset,
149
+ exclude_defaults=exclude_defaults,
150
+ exclude_none=exclude_none,
151
+ warnings=warnings,
152
+ )
153
+
154
+ def to_json(
155
+ self,
156
+ *,
157
+ indent: int | None = 2,
158
+ use_api_names: bool = True,
159
+ exclude_unset: bool = True,
160
+ exclude_defaults: bool = False,
161
+ exclude_none: bool = False,
162
+ warnings: bool = True,
163
+ ) -> str:
164
+ """Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation).
165
+
166
+ By default, fields that were not set by the API will not be included,
167
+ and keys will match the API response, *not* the property names from the model.
168
+
169
+ For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
170
+ the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
171
+
172
+ Args:
173
+ indent: Indentation to use in the JSON output. If `None` is passed, the output will be compact. Defaults to `2`
174
+ use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
175
+ exclude_unset: Whether to exclude fields that have not been explicitly set.
176
+ exclude_defaults: Whether to exclude fields that have the default value.
177
+ exclude_none: Whether to exclude fields that have a value of `None`.
178
+ warnings: Whether to show any warnings that occurred during serialization. This is only supported in Pydantic v2.
179
+ """
180
+ return self.model_dump_json(
181
+ indent=indent,
182
+ by_alias=use_api_names,
183
+ exclude_unset=exclude_unset,
184
+ exclude_defaults=exclude_defaults,
185
+ exclude_none=exclude_none,
186
+ warnings=warnings,
187
+ )
188
+
189
+ @override
190
+ def __str__(self) -> str:
191
+ # mypy complains about an invalid self arg
192
+ return f"{self.__repr_name__()}({self.__repr_str__(', ')})" # type: ignore[misc]
193
+
194
+ # Override the 'construct' method in a way that supports recursive parsing without validation.
195
+ # Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836.
196
+ @classmethod
197
+ @override
198
+ def construct( # pyright: ignore[reportIncompatibleMethodOverride]
199
+ __cls: Type[ModelT],
200
+ _fields_set: set[str] | None = None,
201
+ **values: object,
202
+ ) -> ModelT:
203
+ m = __cls.__new__(__cls)
204
+ fields_values: dict[str, object] = {}
205
+
206
+ config = get_model_config(__cls)
207
+ populate_by_name = (
208
+ config.allow_population_by_field_name
209
+ if isinstance(config, _ConfigProtocol)
210
+ else config.get("populate_by_name")
211
+ )
212
+
213
+ if _fields_set is None:
214
+ _fields_set = set()
215
+
216
+ model_fields = get_model_fields(__cls)
217
+ for name, field in model_fields.items():
218
+ key = field.alias
219
+ if key is None or (key not in values and populate_by_name):
220
+ key = name
221
+
222
+ if key in values:
223
+ fields_values[name] = _construct_field(value=values[key], field=field, key=key)
224
+ _fields_set.add(name)
225
+ else:
226
+ fields_values[name] = field_get_default(field)
227
+
228
+ extra_field_type = _get_extra_fields_type(__cls)
229
+
230
+ _extra = {}
231
+ for key, value in values.items():
232
+ if key not in model_fields:
233
+ parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
234
+
235
+ if PYDANTIC_V1:
236
+ _fields_set.add(key)
237
+ fields_values[key] = parsed
238
+ else:
239
+ _extra[key] = parsed
240
+
241
+ object.__setattr__(m, "__dict__", fields_values)
242
+
243
+ if PYDANTIC_V1:
244
+ # init_private_attributes() does not exist in v2
245
+ m._init_private_attributes() # type: ignore
246
+
247
+ # copied from Pydantic v1's `construct()` method
248
+ object.__setattr__(m, "__fields_set__", _fields_set)
249
+ else:
250
+ # these properties are copied from Pydantic's `model_construct()` method
251
+ object.__setattr__(m, "__pydantic_private__", None)
252
+ object.__setattr__(m, "__pydantic_extra__", _extra)
253
+ object.__setattr__(m, "__pydantic_fields_set__", _fields_set)
254
+
255
+ return m
256
+
257
+ if not TYPE_CHECKING:
258
+ # type checkers incorrectly complain about this assignment
259
+ # because the type signatures are technically different
260
+ # although not in practice
261
+ model_construct = construct
262
+
263
+ if PYDANTIC_V1:
264
+ # we define aliases for some of the new pydantic v2 methods so
265
+ # that we can just document these methods without having to specify
266
+ # a specific pydantic version as some users may not know which
267
+ # pydantic version they are currently using
268
+
269
+ @override
270
+ def model_dump(
271
+ self,
272
+ *,
273
+ mode: Literal["json", "python"] | str = "python",
274
+ include: IncEx | None = None,
275
+ exclude: IncEx | None = None,
276
+ context: Any | None = None,
277
+ by_alias: bool | None = None,
278
+ exclude_unset: bool = False,
279
+ exclude_defaults: bool = False,
280
+ exclude_none: bool = False,
281
+ exclude_computed_fields: bool = False,
282
+ round_trip: bool = False,
283
+ warnings: bool | Literal["none", "warn", "error"] = True,
284
+ fallback: Callable[[Any], Any] | None = None,
285
+ serialize_as_any: bool = False,
286
+ ) -> dict[str, Any]:
287
+ """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump
288
+
289
+ Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
290
+
291
+ Args:
292
+ mode: The mode in which `to_python` should run.
293
+ If mode is 'json', the output will only contain JSON serializable types.
294
+ If mode is 'python', the output may contain non-JSON-serializable Python objects.
295
+ include: A set of fields to include in the output.
296
+ exclude: A set of fields to exclude from the output.
297
+ context: Additional context to pass to the serializer.
298
+ by_alias: Whether to use the field's alias in the dictionary key if defined.
299
+ exclude_unset: Whether to exclude fields that have not been explicitly set.
300
+ exclude_defaults: Whether to exclude fields that are set to their default value.
301
+ exclude_none: Whether to exclude fields that have a value of `None`.
302
+ exclude_computed_fields: Whether to exclude computed fields.
303
+ While this can be useful for round-tripping, it is usually recommended to use the dedicated
304
+ `round_trip` parameter instead.
305
+ round_trip: If True, dumped values should be valid as input for non-idempotent types such as Json[T].
306
+ warnings: How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
307
+ "error" raises a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError].
308
+ fallback: A function to call when an unknown value is encountered. If not provided,
309
+ a [`PydanticSerializationError`][pydantic_core.PydanticSerializationError] error is raised.
310
+ serialize_as_any: Whether to serialize fields with duck-typing serialization behavior.
311
+
312
+ Returns:
313
+ A dictionary representation of the model.
314
+ """
315
+ if mode not in {"json", "python"}:
316
+ raise ValueError("mode must be either 'json' or 'python'")
317
+ if round_trip != False:
318
+ raise ValueError("round_trip is only supported in Pydantic v2")
319
+ if warnings != True:
320
+ raise ValueError("warnings is only supported in Pydantic v2")
321
+ if context is not None:
322
+ raise ValueError("context is only supported in Pydantic v2")
323
+ if serialize_as_any != False:
324
+ raise ValueError("serialize_as_any is only supported in Pydantic v2")
325
+ if fallback is not None:
326
+ raise ValueError("fallback is only supported in Pydantic v2")
327
+ if exclude_computed_fields != False:
328
+ raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
329
+ dumped = super().dict( # pyright: ignore[reportDeprecated]
330
+ include=include,
331
+ exclude=exclude,
332
+ by_alias=by_alias if by_alias is not None else False,
333
+ exclude_unset=exclude_unset,
334
+ exclude_defaults=exclude_defaults,
335
+ exclude_none=exclude_none,
336
+ )
337
+
338
+ return cast("dict[str, Any]", json_safe(dumped)) if mode == "json" else dumped
339
+
340
+ @override
341
+ def model_dump_json(
342
+ self,
343
+ *,
344
+ indent: int | None = None,
345
+ ensure_ascii: bool = False,
346
+ include: IncEx | None = None,
347
+ exclude: IncEx | None = None,
348
+ context: Any | None = None,
349
+ by_alias: bool | None = None,
350
+ exclude_unset: bool = False,
351
+ exclude_defaults: bool = False,
352
+ exclude_none: bool = False,
353
+ exclude_computed_fields: bool = False,
354
+ round_trip: bool = False,
355
+ warnings: bool | Literal["none", "warn", "error"] = True,
356
+ fallback: Callable[[Any], Any] | None = None,
357
+ serialize_as_any: bool = False,
358
+ ) -> str:
359
+ """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump_json
360
+
361
+ Generates a JSON representation of the model using Pydantic's `to_json` method.
362
+
363
+ Args:
364
+ indent: Indentation to use in the JSON output. If None is passed, the output will be compact.
365
+ include: Field(s) to include in the JSON output. Can take either a string or set of strings.
366
+ exclude: Field(s) to exclude from the JSON output. Can take either a string or set of strings.
367
+ by_alias: Whether to serialize using field aliases.
368
+ exclude_unset: Whether to exclude fields that have not been explicitly set.
369
+ exclude_defaults: Whether to exclude fields that have the default value.
370
+ exclude_none: Whether to exclude fields that have a value of `None`.
371
+ round_trip: Whether to use serialization/deserialization between JSON and class instance.
372
+ warnings: Whether to show any warnings that occurred during serialization.
373
+
374
+ Returns:
375
+ A JSON string representation of the model.
376
+ """
377
+ if round_trip != False:
378
+ raise ValueError("round_trip is only supported in Pydantic v2")
379
+ if warnings != True:
380
+ raise ValueError("warnings is only supported in Pydantic v2")
381
+ if context is not None:
382
+ raise ValueError("context is only supported in Pydantic v2")
383
+ if serialize_as_any != False:
384
+ raise ValueError("serialize_as_any is only supported in Pydantic v2")
385
+ if fallback is not None:
386
+ raise ValueError("fallback is only supported in Pydantic v2")
387
+ if ensure_ascii != False:
388
+ raise ValueError("ensure_ascii is only supported in Pydantic v2")
389
+ if exclude_computed_fields != False:
390
+ raise ValueError("exclude_computed_fields is only supported in Pydantic v2")
391
+ return super().json( # type: ignore[reportDeprecated]
392
+ indent=indent,
393
+ include=include,
394
+ exclude=exclude,
395
+ by_alias=by_alias if by_alias is not None else False,
396
+ exclude_unset=exclude_unset,
397
+ exclude_defaults=exclude_defaults,
398
+ exclude_none=exclude_none,
399
+ )
400
+
401
+
402
+ def _construct_field(value: object, field: FieldInfo, key: str) -> object:
403
+ if value is None:
404
+ return field_get_default(field)
405
+
406
+ if PYDANTIC_V1:
407
+ type_ = cast(type, field.outer_type_) # type: ignore
408
+ else:
409
+ type_ = field.annotation # type: ignore
410
+
411
+ if type_ is None:
412
+ raise RuntimeError(f"Unexpected field type is None for {key}")
413
+
414
+ return construct_type(value=value, type_=type_, metadata=getattr(field, "metadata", None))
415
+
416
+
417
+ def _get_extra_fields_type(cls: type[pydantic.BaseModel]) -> type | None:
418
+ if PYDANTIC_V1:
419
+ # TODO
420
+ return None
421
+
422
+ schema = cls.__pydantic_core_schema__
423
+ if schema["type"] == "model":
424
+ fields = schema["schema"]
425
+ if fields["type"] == "model-fields":
426
+ extras = fields.get("extras_schema")
427
+ if extras and "cls" in extras:
428
+ # mypy can't narrow the type
429
+ return extras["cls"] # type: ignore[no-any-return]
430
+
431
+ return None
432
+
433
+
434
+ def is_basemodel(type_: type) -> bool:
435
+ """Returns whether or not the given type is either a `BaseModel` or a union of `BaseModel`"""
436
+ if is_union(type_):
437
+ for variant in get_args(type_):
438
+ if is_basemodel(variant):
439
+ return True
440
+
441
+ return False
442
+
443
+ return is_basemodel_type(type_)
444
+
445
+
446
+ def is_basemodel_type(type_: type) -> TypeGuard[type[BaseModel] | type[GenericModel]]:
447
+ origin = get_origin(type_) or type_
448
+ if not inspect.isclass(origin):
449
+ return False
450
+ return issubclass(origin, BaseModel) or issubclass(origin, GenericModel)
451
+
452
+
453
+ def build(
454
+ base_model_cls: Callable[P, _BaseModelT],
455
+ *args: P.args,
456
+ **kwargs: P.kwargs,
457
+ ) -> _BaseModelT:
458
+ """Construct a BaseModel class without validation.
459
+
460
+ This is useful for cases where you need to instantiate a `BaseModel`
461
+ from an API response as this provides type-safe params which isn't supported
462
+ by helpers like `construct_type()`.
463
+
464
+ ```py
465
+ build(MyModel, my_field_a="foo", my_field_b=123)
466
+ ```
467
+ """
468
+ if args:
469
+ raise TypeError(
470
+ "Received positional arguments which are not supported; Keyword arguments must be used instead",
471
+ )
472
+
473
+ return cast(_BaseModelT, construct_type(type_=base_model_cls, value=kwargs))
474
+
475
+
476
+ def construct_type_unchecked(*, value: object, type_: type[_T]) -> _T:
477
+ """Loose coercion to the expected type with construction of nested values.
478
+
479
+ Note: the returned value from this function is not guaranteed to match the
480
+ given type.
481
+ """
482
+ return cast(_T, construct_type(value=value, type_=type_))
483
+
484
+
485
+ def construct_type(*, value: object, type_: object, metadata: Optional[List[Any]] = None) -> object:
486
+ """Loose coercion to the expected type with construction of nested values.
487
+
488
+ If the given value does not match the expected type then it is returned as-is.
489
+ """
490
+
491
+ # store a reference to the original type we were given before we extract any inner
492
+ # types so that we can properly resolve forward references in `TypeAliasType` annotations
493
+ original_type = None
494
+
495
+ # we allow `object` as the input type because otherwise, passing things like
496
+ # `Literal['value']` will be reported as a type error by type checkers
497
+ type_ = cast("type[object]", type_)
498
+ if is_type_alias_type(type_):
499
+ original_type = type_ # type: ignore[unreachable]
500
+ type_ = type_.__value__ # type: ignore[unreachable]
501
+
502
+ # unwrap `Annotated[T, ...]` -> `T`
503
+ if metadata is not None and len(metadata) > 0:
504
+ meta: tuple[Any, ...] = tuple(metadata)
505
+ elif is_annotated_type(type_):
506
+ meta = get_args(type_)[1:]
507
+ type_ = extract_type_arg(type_, 0)
508
+ else:
509
+ meta = tuple()
510
+
511
+ # we need to use the origin class for any types that are subscripted generics
512
+ # e.g. Dict[str, object]
513
+ origin = get_origin(type_) or type_
514
+ args = get_args(type_)
515
+
516
+ if is_union(origin):
517
+ try:
518
+ return validate_type(type_=cast("type[object]", original_type or type_), value=value)
519
+ except Exception:
520
+ pass
521
+
522
+ # if the type is a discriminated union then we want to construct the right variant
523
+ # in the union, even if the data doesn't match exactly, otherwise we'd break code
524
+ # that relies on the constructed class types, e.g.
525
+ #
526
+ # class FooType:
527
+ # kind: Literal['foo']
528
+ # value: str
529
+ #
530
+ # class BarType:
531
+ # kind: Literal['bar']
532
+ # value: int
533
+ #
534
+ # without this block, if the data we get is something like `{'kind': 'bar', 'value': 'foo'}` then
535
+ # we'd end up constructing `FooType` when it should be `BarType`.
536
+ discriminator = _build_discriminated_union_meta(union=type_, meta_annotations=meta)
537
+ if discriminator and is_mapping(value):
538
+ variant_value = value.get(discriminator.field_alias_from or discriminator.field_name)
539
+ if variant_value and isinstance(variant_value, str):
540
+ variant_type = discriminator.mapping.get(variant_value)
541
+ if variant_type:
542
+ return construct_type(type_=variant_type, value=value)
543
+
544
+ # if the data is not valid, use the first variant that doesn't fail while deserializing
545
+ for variant in args:
546
+ try:
547
+ return construct_type(value=value, type_=variant)
548
+ except Exception:
549
+ continue
550
+
551
+ raise RuntimeError(f"Could not convert data into a valid instance of {type_}")
552
+
553
+ if origin == dict:
554
+ if not is_mapping(value):
555
+ return value
556
+
557
+ _, items_type = get_args(type_) # Dict[_, items_type]
558
+ return {key: construct_type(value=item, type_=items_type) for key, item in value.items()}
559
+
560
+ if (
561
+ not is_literal_type(type_)
562
+ and inspect.isclass(origin)
563
+ and (issubclass(origin, BaseModel) or issubclass(origin, GenericModel))
564
+ ):
565
+ if is_list(value):
566
+ return [cast(Any, type_).construct(**entry) if is_mapping(entry) else entry for entry in value]
567
+
568
+ if is_mapping(value):
569
+ if issubclass(type_, BaseModel):
570
+ return type_.construct(**value) # type: ignore[arg-type]
571
+
572
+ return cast(Any, type_).construct(**value)
573
+
574
+ if origin == list:
575
+ if not is_list(value):
576
+ return value
577
+
578
+ inner_type = args[0] # List[inner_type]
579
+ return [construct_type(value=entry, type_=inner_type) for entry in value]
580
+
581
+ if origin == float:
582
+ if isinstance(value, int):
583
+ coerced = float(value)
584
+ if coerced != value:
585
+ return value
586
+ return coerced
587
+
588
+ return value
589
+
590
+ if type_ == datetime:
591
+ try:
592
+ return parse_datetime(value) # type: ignore
593
+ except Exception:
594
+ return value
595
+
596
+ if type_ == date:
597
+ try:
598
+ return parse_date(value) # type: ignore
599
+ except Exception:
600
+ return value
601
+
602
+ return value
603
+
604
+
605
+ @runtime_checkable
606
+ class CachedDiscriminatorType(Protocol):
607
+ __discriminator__: DiscriminatorDetails
608
+
609
+
610
+ DISCRIMINATOR_CACHE: weakref.WeakKeyDictionary[type, DiscriminatorDetails] = weakref.WeakKeyDictionary()
611
+
612
+
613
+ class DiscriminatorDetails:
614
+ field_name: str
615
+ """The name of the discriminator field in the variant class, e.g.
616
+
617
+ ```py
618
+ class Foo(BaseModel):
619
+ type: Literal['foo']
620
+ ```
621
+
622
+ Will result in field_name='type'
623
+ """
624
+
625
+ field_alias_from: str | None
626
+ """The name of the discriminator field in the API response, e.g.
627
+
628
+ ```py
629
+ class Foo(BaseModel):
630
+ type: Literal['foo'] = Field(alias='type_from_api')
631
+ ```
632
+
633
+ Will result in field_alias_from='type_from_api'
634
+ """
635
+
636
+ mapping: dict[str, type]
637
+ """Mapping of discriminator value to variant type, e.g.
638
+
639
+ {'foo': FooVariant, 'bar': BarVariant}
640
+ """
641
+
642
+ def __init__(
643
+ self,
644
+ *,
645
+ mapping: dict[str, type],
646
+ discriminator_field: str,
647
+ discriminator_alias: str | None,
648
+ ) -> None:
649
+ self.mapping = mapping
650
+ self.field_name = discriminator_field
651
+ self.field_alias_from = discriminator_alias
652
+
653
+
654
+ def _build_discriminated_union_meta(*, union: type, meta_annotations: tuple[Any, ...]) -> DiscriminatorDetails | None:
655
+ cached = DISCRIMINATOR_CACHE.get(union)
656
+ if cached is not None:
657
+ return cached
658
+
659
+ discriminator_field_name: str | None = None
660
+
661
+ for annotation in meta_annotations:
662
+ if isinstance(annotation, PropertyInfo) and annotation.discriminator is not None:
663
+ discriminator_field_name = annotation.discriminator
664
+ break
665
+
666
+ if not discriminator_field_name:
667
+ return None
668
+
669
+ mapping: dict[str, type] = {}
670
+ discriminator_alias: str | None = None
671
+
672
+ for variant in get_args(union):
673
+ variant = strip_annotated_type(variant)
674
+ if is_basemodel_type(variant):
675
+ if PYDANTIC_V1:
676
+ field_info = cast("dict[str, FieldInfo]", variant.__fields__).get(discriminator_field_name) # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
677
+ if not field_info:
678
+ continue
679
+
680
+ # Note: if one variant defines an alias then they all should
681
+ discriminator_alias = field_info.alias
682
+
683
+ if (annotation := getattr(field_info, "annotation", None)) and is_literal_type(annotation):
684
+ for entry in get_args(annotation):
685
+ if isinstance(entry, str):
686
+ mapping[entry] = variant
687
+ else:
688
+ field = _extract_field_schema_pv2(variant, discriminator_field_name)
689
+ if not field:
690
+ continue
691
+
692
+ # Note: if one variant defines an alias then they all should
693
+ discriminator_alias = field.get("serialization_alias")
694
+
695
+ field_schema = field["schema"]
696
+
697
+ if field_schema["type"] == "literal":
698
+ for entry in cast("LiteralSchema", field_schema)["expected"]:
699
+ if isinstance(entry, str):
700
+ mapping[entry] = variant
701
+
702
+ if not mapping:
703
+ return None
704
+
705
+ details = DiscriminatorDetails(
706
+ mapping=mapping,
707
+ discriminator_field=discriminator_field_name,
708
+ discriminator_alias=discriminator_alias,
709
+ )
710
+ DISCRIMINATOR_CACHE.setdefault(union, details)
711
+ return details
712
+
713
+
714
+ def _extract_field_schema_pv2(model: type[BaseModel], field_name: str) -> ModelField | None:
715
+ schema = model.__pydantic_core_schema__
716
+ if schema["type"] == "definitions":
717
+ schema = schema["schema"]
718
+
719
+ if schema["type"] != "model":
720
+ return None
721
+
722
+ schema = cast("ModelSchema", schema)
723
+ fields_schema = schema["schema"]
724
+ if fields_schema["type"] != "model-fields":
725
+ return None
726
+
727
+ fields_schema = cast("ModelFieldsSchema", fields_schema)
728
+ field = fields_schema["fields"].get(field_name)
729
+ if not field:
730
+ return None
731
+
732
+ return cast("ModelField", field) # pyright: ignore[reportUnnecessaryCast]
733
+
734
+
735
+ def validate_type(*, type_: type[_T], value: object) -> _T:
736
+ """Strict validation that the given value matches the expected type"""
737
+ if inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel):
738
+ return cast(_T, parse_obj(type_, value))
739
+
740
+ return cast(_T, _validate_non_model_type(type_=type_, value=value))
741
+
742
+
743
+ def set_pydantic_config(typ: Any, config: pydantic.ConfigDict) -> None:
744
+ """Add a pydantic config for the given type.
745
+
746
+ Note: this is a no-op on Pydantic v1.
747
+ """
748
+ setattr(typ, "__pydantic_config__", config) # noqa: B010
749
+
750
+
751
+ # our use of subclassing here causes weirdness for type checkers,
752
+ # so we just pretend that we don't subclass
753
+ if TYPE_CHECKING:
754
+ GenericModel = BaseModel
755
+ else:
756
+
757
+ class GenericModel(BaseGenericModel, BaseModel):
758
+ pass
759
+
760
+
761
+ if not PYDANTIC_V1:
762
+ from pydantic import TypeAdapter as _TypeAdapter
763
+
764
+ _CachedTypeAdapter = cast("TypeAdapter[object]", lru_cache(maxsize=None)(_TypeAdapter))
765
+
766
+ if TYPE_CHECKING:
767
+ from pydantic import TypeAdapter
768
+ else:
769
+ TypeAdapter = _CachedTypeAdapter
770
+
771
+ def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
772
+ return TypeAdapter(type_).validate_python(value)
773
+
774
+ elif not TYPE_CHECKING: # TODO: condition is weird
775
+
776
+ class RootModel(GenericModel, Generic[_T]):
777
+ """Used as a placeholder to easily convert runtime types to a Pydantic format
778
+ to provide validation.
779
+
780
+ For example:
781
+ ```py
782
+ validated = RootModel[int](__root__="5").__root__
783
+ # validated: 5
784
+ ```
785
+ """
786
+
787
+ __root__: _T
788
+
789
+ def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
790
+ model = _create_pydantic_model(type_).validate(value)
791
+ return cast(_T, model.__root__)
792
+
793
+ def _create_pydantic_model(type_: _T) -> Type[RootModel[_T]]:
794
+ return RootModel[type_] # type: ignore
795
+
796
+
797
+ class FinalRequestOptionsInput(TypedDict, total=False):
798
+ method: Required[str]
799
+ url: Required[str]
800
+ params: Query
801
+ headers: Headers
802
+ max_retries: int
803
+ timeout: float | Timeout | None
804
+ files: HttpxRequestFiles | None
805
+ idempotency_key: str
806
+ json_data: Body
807
+ extra_json: AnyMapping
808
+ follow_redirects: bool
809
+
810
+
811
+ @final
812
+ class FinalRequestOptions(pydantic.BaseModel):
813
+ method: str
814
+ url: str
815
+ params: Query = {}
816
+ headers: Union[Headers, NotGiven] = NotGiven()
817
+ max_retries: Union[int, NotGiven] = NotGiven()
818
+ timeout: Union[float, Timeout, None, NotGiven] = NotGiven()
819
+ files: Union[HttpxRequestFiles, None] = None
820
+ idempotency_key: Union[str, None] = None
821
+ post_parser: Union[Callable[[Any], Any], NotGiven] = NotGiven()
822
+ follow_redirects: Union[bool, None] = None
823
+
824
+ # It should be noted that we cannot use `json` here as that would override
825
+ # a BaseModel method in an incompatible fashion.
826
+ json_data: Union[Body, None] = None
827
+ extra_json: Union[AnyMapping, None] = None
828
+
829
+ if PYDANTIC_V1:
830
+
831
+ class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
832
+ arbitrary_types_allowed: bool = True
833
+ else:
834
+ model_config: ClassVar[ConfigDict] = ConfigDict(arbitrary_types_allowed=True)
835
+
836
+ def get_max_retries(self, max_retries: int) -> int:
837
+ if isinstance(self.max_retries, NotGiven):
838
+ return max_retries
839
+ return self.max_retries
840
+
841
+ def _strip_raw_response_header(self) -> None:
842
+ if not is_given(self.headers):
843
+ return
844
+
845
+ if self.headers.get(RAW_RESPONSE_HEADER):
846
+ self.headers = {**self.headers}
847
+ self.headers.pop(RAW_RESPONSE_HEADER)
848
+
849
+ # override the `construct` method so that we can run custom transformations.
850
+ # this is necessary as we don't want to do any actual runtime type checking
851
+ # (which means we can't use validators) but we do want to ensure that `NotGiven`
852
+ # values are not present
853
+ #
854
+ # type ignore required because we're adding explicit types to `**values`
855
+ @classmethod
856
+ def construct( # type: ignore
857
+ cls,
858
+ _fields_set: set[str] | None = None,
859
+ **values: Unpack[FinalRequestOptionsInput],
860
+ ) -> FinalRequestOptions:
861
+ kwargs: dict[str, Any] = {
862
+ # we unconditionally call `strip_not_given` on any value
863
+ # as it will just ignore any non-mapping types
864
+ key: strip_not_given(value)
865
+ for key, value in values.items()
866
+ }
867
+ if PYDANTIC_V1:
868
+ return cast(FinalRequestOptions, super().construct(_fields_set, **kwargs)) # pyright: ignore[reportDeprecated]
869
+ return super().model_construct(_fields_set, **kwargs)
870
+
871
+ if not TYPE_CHECKING:
872
+ # type checkers incorrectly complain about this assignment
873
+ model_construct = construct