mirascope 2.0.0a2__py3-none-any.whl → 2.0.0a3__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 (204) hide show
  1. mirascope/__init__.py +2 -2
  2. mirascope/api/__init__.py +6 -0
  3. mirascope/api/_generated/README.md +207 -0
  4. mirascope/api/_generated/__init__.py +85 -0
  5. mirascope/api/_generated/client.py +155 -0
  6. mirascope/api/_generated/core/__init__.py +52 -0
  7. mirascope/api/_generated/core/api_error.py +23 -0
  8. mirascope/api/_generated/core/client_wrapper.py +58 -0
  9. mirascope/api/_generated/core/datetime_utils.py +30 -0
  10. mirascope/api/_generated/core/file.py +70 -0
  11. mirascope/api/_generated/core/force_multipart.py +16 -0
  12. mirascope/api/_generated/core/http_client.py +619 -0
  13. mirascope/api/_generated/core/http_response.py +55 -0
  14. mirascope/api/_generated/core/jsonable_encoder.py +102 -0
  15. mirascope/api/_generated/core/pydantic_utilities.py +310 -0
  16. mirascope/api/_generated/core/query_encoder.py +60 -0
  17. mirascope/api/_generated/core/remove_none_from_dict.py +11 -0
  18. mirascope/api/_generated/core/request_options.py +35 -0
  19. mirascope/api/_generated/core/serialization.py +282 -0
  20. mirascope/api/_generated/docs/__init__.py +4 -0
  21. mirascope/api/_generated/docs/client.py +95 -0
  22. mirascope/api/_generated/docs/raw_client.py +132 -0
  23. mirascope/api/_generated/environment.py +9 -0
  24. mirascope/api/_generated/errors/__init__.py +7 -0
  25. mirascope/api/_generated/errors/bad_request_error.py +15 -0
  26. mirascope/api/_generated/health/__init__.py +7 -0
  27. mirascope/api/_generated/health/client.py +96 -0
  28. mirascope/api/_generated/health/raw_client.py +129 -0
  29. mirascope/api/_generated/health/types/__init__.py +8 -0
  30. mirascope/api/_generated/health/types/health_check_response.py +24 -0
  31. mirascope/api/_generated/health/types/health_check_response_status.py +5 -0
  32. mirascope/api/_generated/reference.md +167 -0
  33. mirascope/api/_generated/traces/__init__.py +55 -0
  34. mirascope/api/_generated/traces/client.py +162 -0
  35. mirascope/api/_generated/traces/raw_client.py +168 -0
  36. mirascope/api/_generated/traces/types/__init__.py +95 -0
  37. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item.py +36 -0
  38. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource.py +31 -0
  39. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item.py +25 -0
  40. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value.py +54 -0
  41. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_array_value.py +23 -0
  42. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_kvlist_value.py +28 -0
  43. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_resource_attributes_item_value_kvlist_value_values_item.py +24 -0
  44. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item.py +35 -0
  45. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope.py +35 -0
  46. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item.py +27 -0
  47. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value.py +54 -0
  48. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_array_value.py +23 -0
  49. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_kvlist_value.py +28 -0
  50. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_scope_attributes_item_value_kvlist_value_values_item.py +24 -0
  51. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item.py +60 -0
  52. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item.py +29 -0
  53. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value.py +54 -0
  54. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_array_value.py +23 -0
  55. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_kvlist_value.py +28 -0
  56. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_attributes_item_value_kvlist_value_values_item.py +24 -0
  57. mirascope/api/_generated/traces/types/traces_create_request_resource_spans_item_scope_spans_item_spans_item_status.py +24 -0
  58. mirascope/api/_generated/traces/types/traces_create_response.py +27 -0
  59. mirascope/api/_generated/traces/types/traces_create_response_partial_success.py +28 -0
  60. mirascope/api/_generated/types/__init__.py +21 -0
  61. mirascope/api/_generated/types/http_api_decode_error.py +31 -0
  62. mirascope/api/_generated/types/http_api_decode_error_tag.py +5 -0
  63. mirascope/api/_generated/types/issue.py +44 -0
  64. mirascope/api/_generated/types/issue_tag.py +17 -0
  65. mirascope/api/_generated/types/property_key.py +7 -0
  66. mirascope/api/_generated/types/property_key_tag.py +29 -0
  67. mirascope/api/_generated/types/property_key_tag_tag.py +5 -0
  68. mirascope/api/client.py +255 -0
  69. mirascope/api/settings.py +81 -0
  70. mirascope/llm/__init__.py +41 -11
  71. mirascope/llm/calls/calls.py +81 -57
  72. mirascope/llm/calls/decorator.py +121 -115
  73. mirascope/llm/content/__init__.py +3 -2
  74. mirascope/llm/context/_utils.py +19 -6
  75. mirascope/llm/exceptions.py +30 -16
  76. mirascope/llm/formatting/_utils.py +9 -5
  77. mirascope/llm/formatting/format.py +2 -2
  78. mirascope/llm/formatting/from_call_args.py +2 -2
  79. mirascope/llm/messages/message.py +13 -5
  80. mirascope/llm/models/__init__.py +2 -2
  81. mirascope/llm/models/models.py +189 -81
  82. mirascope/llm/prompts/__init__.py +13 -12
  83. mirascope/llm/prompts/_utils.py +27 -24
  84. mirascope/llm/prompts/decorator.py +133 -204
  85. mirascope/llm/prompts/prompts.py +424 -0
  86. mirascope/llm/prompts/protocols.py +25 -59
  87. mirascope/llm/providers/__init__.py +38 -0
  88. mirascope/llm/{clients → providers}/_missing_import_stubs.py +8 -6
  89. mirascope/llm/providers/anthropic/__init__.py +24 -0
  90. mirascope/llm/{clients → providers}/anthropic/_utils/decode.py +5 -4
  91. mirascope/llm/{clients → providers}/anthropic/_utils/encode.py +31 -10
  92. mirascope/llm/providers/anthropic/model_id.py +40 -0
  93. mirascope/llm/{clients/anthropic/clients.py → providers/anthropic/provider.py} +33 -418
  94. mirascope/llm/{clients → providers}/base/__init__.py +3 -3
  95. mirascope/llm/{clients → providers}/base/_utils.py +10 -7
  96. mirascope/llm/{clients/base/client.py → providers/base/base_provider.py} +255 -126
  97. mirascope/llm/providers/google/__init__.py +21 -0
  98. mirascope/llm/{clients → providers}/google/_utils/decode.py +6 -4
  99. mirascope/llm/{clients → providers}/google/_utils/encode.py +30 -24
  100. mirascope/llm/providers/google/model_id.py +28 -0
  101. mirascope/llm/providers/google/provider.py +438 -0
  102. mirascope/llm/providers/load_provider.py +48 -0
  103. mirascope/llm/providers/mlx/__init__.py +24 -0
  104. mirascope/llm/providers/mlx/_utils.py +107 -0
  105. mirascope/llm/providers/mlx/encoding/__init__.py +8 -0
  106. mirascope/llm/providers/mlx/encoding/base.py +69 -0
  107. mirascope/llm/providers/mlx/encoding/transformers.py +131 -0
  108. mirascope/llm/providers/mlx/mlx.py +237 -0
  109. mirascope/llm/providers/mlx/model_id.py +17 -0
  110. mirascope/llm/providers/mlx/provider.py +411 -0
  111. mirascope/llm/providers/model_id.py +16 -0
  112. mirascope/llm/providers/openai/__init__.py +6 -0
  113. mirascope/llm/providers/openai/completions/__init__.py +20 -0
  114. mirascope/llm/{clients/openai/responses → providers/openai/completions}/_utils/__init__.py +2 -0
  115. mirascope/llm/{clients → providers}/openai/completions/_utils/decode.py +5 -3
  116. mirascope/llm/{clients → providers}/openai/completions/_utils/encode.py +33 -23
  117. mirascope/llm/providers/openai/completions/provider.py +456 -0
  118. mirascope/llm/providers/openai/model_id.py +31 -0
  119. mirascope/llm/providers/openai/model_info.py +246 -0
  120. mirascope/llm/providers/openai/provider.py +386 -0
  121. mirascope/llm/providers/openai/responses/__init__.py +21 -0
  122. mirascope/llm/{clients → providers}/openai/responses/_utils/decode.py +5 -3
  123. mirascope/llm/{clients → providers}/openai/responses/_utils/encode.py +28 -17
  124. mirascope/llm/providers/openai/responses/provider.py +470 -0
  125. mirascope/llm/{clients → providers}/openai/shared/_utils.py +7 -3
  126. mirascope/llm/providers/provider_id.py +13 -0
  127. mirascope/llm/providers/provider_registry.py +167 -0
  128. mirascope/llm/responses/base_response.py +10 -5
  129. mirascope/llm/responses/base_stream_response.py +10 -5
  130. mirascope/llm/responses/response.py +24 -13
  131. mirascope/llm/responses/root_response.py +7 -12
  132. mirascope/llm/responses/stream_response.py +35 -23
  133. mirascope/llm/tools/__init__.py +9 -2
  134. mirascope/llm/tools/_utils.py +12 -3
  135. mirascope/llm/tools/protocols.py +4 -4
  136. mirascope/llm/tools/tool_schema.py +44 -9
  137. mirascope/llm/tools/tools.py +10 -9
  138. mirascope/ops/__init__.py +156 -0
  139. mirascope/ops/_internal/__init__.py +5 -0
  140. mirascope/ops/_internal/closure.py +1118 -0
  141. mirascope/ops/_internal/configuration.py +126 -0
  142. mirascope/ops/_internal/context.py +76 -0
  143. mirascope/ops/_internal/exporters/__init__.py +26 -0
  144. mirascope/ops/_internal/exporters/exporters.py +342 -0
  145. mirascope/ops/_internal/exporters/processors.py +104 -0
  146. mirascope/ops/_internal/exporters/types.py +165 -0
  147. mirascope/ops/_internal/exporters/utils.py +29 -0
  148. mirascope/ops/_internal/instrumentation/__init__.py +8 -0
  149. mirascope/ops/_internal/instrumentation/llm/__init__.py +8 -0
  150. mirascope/ops/_internal/instrumentation/llm/encode.py +238 -0
  151. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/__init__.py +38 -0
  152. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_input_messages.py +31 -0
  153. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_output_messages.py +38 -0
  154. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_system_instructions.py +18 -0
  155. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/shared.py +100 -0
  156. mirascope/ops/_internal/instrumentation/llm/llm.py +1288 -0
  157. mirascope/ops/_internal/propagation.py +198 -0
  158. mirascope/ops/_internal/protocols.py +51 -0
  159. mirascope/ops/_internal/session.py +139 -0
  160. mirascope/ops/_internal/spans.py +232 -0
  161. mirascope/ops/_internal/traced_calls.py +371 -0
  162. mirascope/ops/_internal/traced_functions.py +394 -0
  163. mirascope/ops/_internal/tracing.py +276 -0
  164. mirascope/ops/_internal/types.py +13 -0
  165. mirascope/ops/_internal/utils.py +75 -0
  166. mirascope/ops/_internal/versioned_calls.py +512 -0
  167. mirascope/ops/_internal/versioned_functions.py +346 -0
  168. mirascope/ops/_internal/versioning.py +303 -0
  169. mirascope/ops/exceptions.py +21 -0
  170. {mirascope-2.0.0a2.dist-info → mirascope-2.0.0a3.dist-info}/METADATA +76 -1
  171. mirascope-2.0.0a3.dist-info/RECORD +206 -0
  172. {mirascope-2.0.0a2.dist-info → mirascope-2.0.0a3.dist-info}/WHEEL +1 -1
  173. mirascope/graphs/__init__.py +0 -22
  174. mirascope/graphs/finite_state_machine.py +0 -625
  175. mirascope/llm/agents/__init__.py +0 -15
  176. mirascope/llm/agents/agent.py +0 -97
  177. mirascope/llm/agents/agent_template.py +0 -45
  178. mirascope/llm/agents/decorator.py +0 -176
  179. mirascope/llm/calls/base_call.py +0 -33
  180. mirascope/llm/clients/__init__.py +0 -34
  181. mirascope/llm/clients/anthropic/__init__.py +0 -25
  182. mirascope/llm/clients/anthropic/model_ids.py +0 -8
  183. mirascope/llm/clients/google/__init__.py +0 -20
  184. mirascope/llm/clients/google/clients.py +0 -853
  185. mirascope/llm/clients/google/model_ids.py +0 -15
  186. mirascope/llm/clients/openai/__init__.py +0 -25
  187. mirascope/llm/clients/openai/completions/__init__.py +0 -28
  188. mirascope/llm/clients/openai/completions/_utils/model_features.py +0 -81
  189. mirascope/llm/clients/openai/completions/clients.py +0 -833
  190. mirascope/llm/clients/openai/completions/model_ids.py +0 -8
  191. mirascope/llm/clients/openai/responses/__init__.py +0 -26
  192. mirascope/llm/clients/openai/responses/_utils/model_features.py +0 -87
  193. mirascope/llm/clients/openai/responses/clients.py +0 -832
  194. mirascope/llm/clients/openai/responses/model_ids.py +0 -8
  195. mirascope/llm/clients/providers.py +0 -175
  196. mirascope-2.0.0a2.dist-info/RECORD +0 -102
  197. /mirascope/llm/{clients → providers}/anthropic/_utils/__init__.py +0 -0
  198. /mirascope/llm/{clients → providers}/base/kwargs.py +0 -0
  199. /mirascope/llm/{clients → providers}/base/params.py +0 -0
  200. /mirascope/llm/{clients → providers}/google/_utils/__init__.py +0 -0
  201. /mirascope/llm/{clients → providers}/google/message.py +0 -0
  202. /mirascope/llm/{clients/openai/completions → providers/openai/responses}/_utils/__init__.py +0 -0
  203. /mirascope/llm/{clients → providers}/openai/shared/__init__.py +0 -0
  204. {mirascope-2.0.0a2.dist-info → mirascope-2.0.0a3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,55 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ from typing import Dict, Generic, TypeVar
4
+
5
+ import httpx
6
+
7
+ T = TypeVar("T")
8
+ """Generic to represent the underlying type of the data wrapped by the HTTP response."""
9
+
10
+
11
+ class BaseHttpResponse:
12
+ """Minimalist HTTP response wrapper that exposes response headers."""
13
+
14
+ _response: httpx.Response
15
+
16
+ def __init__(self, response: httpx.Response):
17
+ self._response = response
18
+
19
+ @property
20
+ def headers(self) -> Dict[str, str]:
21
+ return dict(self._response.headers)
22
+
23
+
24
+ class HttpResponse(Generic[T], BaseHttpResponse):
25
+ """HTTP response wrapper that exposes response headers and data."""
26
+
27
+ _data: T
28
+
29
+ def __init__(self, response: httpx.Response, data: T):
30
+ super().__init__(response)
31
+ self._data = data
32
+
33
+ @property
34
+ def data(self) -> T:
35
+ return self._data
36
+
37
+ def close(self) -> None:
38
+ self._response.close()
39
+
40
+
41
+ class AsyncHttpResponse(Generic[T], BaseHttpResponse):
42
+ """HTTP response wrapper that exposes response headers and data."""
43
+
44
+ _data: T
45
+
46
+ def __init__(self, response: httpx.Response, data: T):
47
+ super().__init__(response)
48
+ self._data = data
49
+
50
+ @property
51
+ def data(self) -> T:
52
+ return self._data
53
+
54
+ async def close(self) -> None:
55
+ await self._response.aclose()
@@ -0,0 +1,102 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ """
4
+ jsonable_encoder converts a Python object to a JSON-friendly dict
5
+ (e.g. datetimes to strings, Pydantic models to dicts).
6
+
7
+ Taken from FastAPI, and made a bit simpler
8
+ https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py
9
+ """
10
+
11
+ import base64
12
+ import dataclasses
13
+ import datetime as dt
14
+ from enum import Enum
15
+ from pathlib import PurePath
16
+ from types import GeneratorType
17
+ from typing import Any, Callable, Dict, List, Optional, Set, Union
18
+
19
+ import pydantic
20
+ from .datetime_utils import serialize_datetime
21
+ from .pydantic_utilities import (
22
+ IS_PYDANTIC_V2,
23
+ encode_by_type,
24
+ to_jsonable_with_fallback,
25
+ )
26
+
27
+ SetIntStr = Set[Union[int, str]]
28
+ DictIntStrAny = Dict[Union[int, str], Any]
29
+
30
+
31
+ def jsonable_encoder(
32
+ obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any], Any]]] = None
33
+ ) -> Any:
34
+ custom_encoder = custom_encoder or {}
35
+ if custom_encoder:
36
+ if type(obj) in custom_encoder:
37
+ return custom_encoder[type(obj)](obj)
38
+ else:
39
+ for encoder_type, encoder_instance in custom_encoder.items():
40
+ if isinstance(obj, encoder_type):
41
+ return encoder_instance(obj)
42
+ if isinstance(obj, pydantic.BaseModel):
43
+ if IS_PYDANTIC_V2:
44
+ encoder = getattr(obj.model_config, "json_encoders", {}) # type: ignore # Pydantic v2
45
+ else:
46
+ encoder = getattr(obj.__config__, "json_encoders", {}) # type: ignore # Pydantic v1
47
+ if custom_encoder:
48
+ encoder.update(custom_encoder)
49
+ obj_dict = obj.dict(by_alias=True)
50
+ if "__root__" in obj_dict:
51
+ obj_dict = obj_dict["__root__"]
52
+ if "root" in obj_dict:
53
+ obj_dict = obj_dict["root"]
54
+ return jsonable_encoder(obj_dict, custom_encoder=encoder)
55
+ if dataclasses.is_dataclass(obj):
56
+ obj_dict = dataclasses.asdict(obj) # type: ignore
57
+ return jsonable_encoder(obj_dict, custom_encoder=custom_encoder)
58
+ if isinstance(obj, bytes):
59
+ return base64.b64encode(obj).decode("utf-8")
60
+ if isinstance(obj, Enum):
61
+ return obj.value
62
+ if isinstance(obj, PurePath):
63
+ return str(obj)
64
+ if isinstance(obj, (str, int, float, type(None))):
65
+ return obj
66
+ if isinstance(obj, dt.datetime):
67
+ return serialize_datetime(obj)
68
+ if isinstance(obj, dt.date):
69
+ return str(obj)
70
+ if isinstance(obj, dict):
71
+ encoded_dict = {}
72
+ allowed_keys = set(obj.keys())
73
+ for key, value in obj.items():
74
+ if key in allowed_keys:
75
+ encoded_key = jsonable_encoder(key, custom_encoder=custom_encoder)
76
+ encoded_value = jsonable_encoder(value, custom_encoder=custom_encoder)
77
+ encoded_dict[encoded_key] = encoded_value
78
+ return encoded_dict
79
+ if isinstance(obj, (list, set, frozenset, GeneratorType, tuple)):
80
+ encoded_list = []
81
+ for item in obj:
82
+ encoded_list.append(jsonable_encoder(item, custom_encoder=custom_encoder))
83
+ return encoded_list
84
+
85
+ def fallback_serializer(o: Any) -> Any:
86
+ attempt_encode = encode_by_type(o)
87
+ if attempt_encode is not None:
88
+ return attempt_encode
89
+
90
+ try:
91
+ data = dict(o)
92
+ except Exception as e:
93
+ errors: List[Exception] = []
94
+ errors.append(e)
95
+ try:
96
+ data = vars(o)
97
+ except Exception as e:
98
+ errors.append(e)
99
+ raise ValueError(errors) from e
100
+ return jsonable_encoder(data, custom_encoder=custom_encoder)
101
+
102
+ return to_jsonable_with_fallback(obj, fallback_serializer)
@@ -0,0 +1,310 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ # nopycln: file
4
+ import datetime as dt
5
+ from collections import defaultdict
6
+ from typing import (
7
+ Any,
8
+ Callable,
9
+ ClassVar,
10
+ Dict,
11
+ List,
12
+ Mapping,
13
+ Optional,
14
+ Set,
15
+ Tuple,
16
+ Type,
17
+ TypeVar,
18
+ Union,
19
+ cast,
20
+ )
21
+
22
+ import pydantic
23
+
24
+ IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
25
+
26
+ if IS_PYDANTIC_V2:
27
+ from pydantic.v1.datetime_parse import parse_date as parse_date
28
+ from pydantic.v1.datetime_parse import parse_datetime as parse_datetime
29
+ from pydantic.v1.fields import ModelField as ModelField
30
+ from pydantic.v1.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[attr-defined]
31
+ from pydantic.v1.typing import get_args as get_args
32
+ from pydantic.v1.typing import get_origin as get_origin
33
+ from pydantic.v1.typing import is_literal_type as is_literal_type
34
+ from pydantic.v1.typing import is_union as is_union
35
+ else:
36
+ from pydantic.datetime_parse import parse_date as parse_date # type: ignore[no-redef]
37
+ from pydantic.datetime_parse import parse_datetime as parse_datetime # type: ignore[no-redef]
38
+ from pydantic.fields import ModelField as ModelField # type: ignore[attr-defined, no-redef]
39
+ from pydantic.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore[no-redef]
40
+ from pydantic.typing import get_args as get_args # type: ignore[no-redef]
41
+ from pydantic.typing import get_origin as get_origin # type: ignore[no-redef]
42
+ from pydantic.typing import is_literal_type as is_literal_type # type: ignore[no-redef]
43
+ from pydantic.typing import is_union as is_union # type: ignore[no-redef]
44
+
45
+ from .datetime_utils import serialize_datetime
46
+ from .serialization import convert_and_respect_annotation_metadata
47
+ from typing_extensions import TypeAlias
48
+
49
+ T = TypeVar("T")
50
+ Model = TypeVar("Model", bound=pydantic.BaseModel)
51
+
52
+
53
+ def parse_obj_as(type_: Type[T], object_: Any) -> T:
54
+ dealiased_object = convert_and_respect_annotation_metadata(
55
+ object_=object_, annotation=type_, direction="read"
56
+ )
57
+ if IS_PYDANTIC_V2:
58
+ adapter = pydantic.TypeAdapter(type_) # type: ignore[attr-defined]
59
+ return adapter.validate_python(dealiased_object)
60
+ return pydantic.parse_obj_as(type_, dealiased_object)
61
+
62
+
63
+ def to_jsonable_with_fallback(
64
+ obj: Any, fallback_serializer: Callable[[Any], Any]
65
+ ) -> Any:
66
+ if IS_PYDANTIC_V2:
67
+ from pydantic_core import to_jsonable_python
68
+
69
+ return to_jsonable_python(obj, fallback=fallback_serializer)
70
+ return fallback_serializer(obj)
71
+
72
+
73
+ class UniversalBaseModel(pydantic.BaseModel):
74
+ if IS_PYDANTIC_V2:
75
+ model_config: ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict( # type: ignore[typeddict-unknown-key]
76
+ # Allow fields beginning with `model_` to be used in the model
77
+ protected_namespaces=(),
78
+ )
79
+
80
+ @pydantic.model_serializer(mode="wrap", when_used="json") # type: ignore[attr-defined]
81
+ def serialize_model(
82
+ self, handler: pydantic.SerializerFunctionWrapHandler
83
+ ) -> Any: # type: ignore[name-defined]
84
+ serialized = handler(self)
85
+ data = {
86
+ k: serialize_datetime(v) if isinstance(v, dt.datetime) else v
87
+ for k, v in serialized.items()
88
+ }
89
+ return data
90
+
91
+ else:
92
+
93
+ class Config:
94
+ smart_union = True
95
+ json_encoders = {dt.datetime: serialize_datetime}
96
+
97
+ @classmethod
98
+ def model_construct(
99
+ cls: Type["Model"], _fields_set: Optional[Set[str]] = None, **values: Any
100
+ ) -> "Model":
101
+ dealiased_object = convert_and_respect_annotation_metadata(
102
+ object_=values, annotation=cls, direction="read"
103
+ )
104
+ return cls.construct(_fields_set, **dealiased_object)
105
+
106
+ @classmethod
107
+ def construct(
108
+ cls: Type["Model"], _fields_set: Optional[Set[str]] = None, **values: Any
109
+ ) -> "Model":
110
+ dealiased_object = convert_and_respect_annotation_metadata(
111
+ object_=values, annotation=cls, direction="read"
112
+ )
113
+ if IS_PYDANTIC_V2:
114
+ return super().model_construct(_fields_set, **dealiased_object) # type: ignore[misc]
115
+ return super().construct(_fields_set, **dealiased_object)
116
+
117
+ def json(self, **kwargs: Any) -> str:
118
+ kwargs_with_defaults = {
119
+ "by_alias": True,
120
+ "exclude_unset": True,
121
+ **kwargs,
122
+ }
123
+ if IS_PYDANTIC_V2:
124
+ return super().model_dump_json(**kwargs_with_defaults) # type: ignore[misc]
125
+ return super().json(**kwargs_with_defaults)
126
+
127
+ def dict(self, **kwargs: Any) -> Dict[str, Any]:
128
+ """
129
+ Override the default dict method to `exclude_unset` by default. This function patches
130
+ `exclude_unset` to work include fields within non-None default values.
131
+ """
132
+ # Note: the logic here is multiplexed given the levers exposed in Pydantic V1 vs V2
133
+ # Pydantic V1's .dict can be extremely slow, so we do not want to call it twice.
134
+ #
135
+ # We'd ideally do the same for Pydantic V2, but it shells out to a library to serialize models
136
+ # that we have less control over, and this is less intrusive than custom serializers for now.
137
+ if IS_PYDANTIC_V2:
138
+ kwargs_with_defaults_exclude_unset = {
139
+ **kwargs,
140
+ "by_alias": True,
141
+ "exclude_unset": True,
142
+ "exclude_none": False,
143
+ }
144
+ kwargs_with_defaults_exclude_none = {
145
+ **kwargs,
146
+ "by_alias": True,
147
+ "exclude_none": True,
148
+ "exclude_unset": False,
149
+ }
150
+ dict_dump = deep_union_pydantic_dicts(
151
+ super().model_dump(**kwargs_with_defaults_exclude_unset), # type: ignore[misc]
152
+ super().model_dump(**kwargs_with_defaults_exclude_none), # type: ignore[misc]
153
+ )
154
+
155
+ else:
156
+ _fields_set = self.__fields_set__.copy()
157
+
158
+ fields = _get_model_fields(self.__class__)
159
+ for name, field in fields.items():
160
+ if name not in _fields_set:
161
+ default = _get_field_default(field)
162
+
163
+ # If the default values are non-null act like they've been set
164
+ # This effectively allows exclude_unset to work like exclude_none where
165
+ # the latter passes through intentionally set none values.
166
+ if default is not None or (
167
+ "exclude_unset" in kwargs and not kwargs["exclude_unset"]
168
+ ):
169
+ _fields_set.add(name)
170
+
171
+ if default is not None:
172
+ self.__fields_set__.add(name)
173
+
174
+ kwargs_with_defaults_exclude_unset_include_fields = {
175
+ "by_alias": True,
176
+ "exclude_unset": True,
177
+ "include": _fields_set,
178
+ **kwargs,
179
+ }
180
+
181
+ dict_dump = super().dict(
182
+ **kwargs_with_defaults_exclude_unset_include_fields
183
+ )
184
+
185
+ return convert_and_respect_annotation_metadata(
186
+ object_=dict_dump, annotation=self.__class__, direction="write"
187
+ )
188
+
189
+
190
+ def _union_list_of_pydantic_dicts(
191
+ source: List[Any], destination: List[Any]
192
+ ) -> List[Any]:
193
+ converted_list: List[Any] = []
194
+ for i, item in enumerate(source):
195
+ destination_value = destination[i]
196
+ if isinstance(item, dict):
197
+ converted_list.append(deep_union_pydantic_dicts(item, destination_value))
198
+ elif isinstance(item, list):
199
+ converted_list.append(
200
+ _union_list_of_pydantic_dicts(item, destination_value)
201
+ )
202
+ else:
203
+ converted_list.append(item)
204
+ return converted_list
205
+
206
+
207
+ def deep_union_pydantic_dicts(
208
+ source: Dict[str, Any], destination: Dict[str, Any]
209
+ ) -> Dict[str, Any]:
210
+ for key, value in source.items():
211
+ node = destination.setdefault(key, {})
212
+ if isinstance(value, dict):
213
+ deep_union_pydantic_dicts(value, node)
214
+ # Note: we do not do this same processing for sets given we do not have sets of models
215
+ # and given the sets are unordered, the processing of the set and matching objects would
216
+ # be non-trivial.
217
+ elif isinstance(value, list):
218
+ destination[key] = _union_list_of_pydantic_dicts(value, node)
219
+ else:
220
+ destination[key] = value
221
+
222
+ return destination
223
+
224
+
225
+ if IS_PYDANTIC_V2:
226
+
227
+ class V2RootModel(UniversalBaseModel, pydantic.RootModel): # type: ignore[misc, name-defined, type-arg]
228
+ pass
229
+
230
+ UniversalRootModel: TypeAlias = V2RootModel # type: ignore[misc]
231
+ else:
232
+ UniversalRootModel: TypeAlias = UniversalBaseModel # type: ignore[misc, no-redef]
233
+
234
+
235
+ def encode_by_type(o: Any) -> Any:
236
+ encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(
237
+ tuple
238
+ )
239
+ for type_, encoder in encoders_by_type.items():
240
+ encoders_by_class_tuples[encoder] += (type_,)
241
+
242
+ if type(o) in encoders_by_type:
243
+ return encoders_by_type[type(o)](o)
244
+ for encoder, classes_tuple in encoders_by_class_tuples.items():
245
+ if isinstance(o, classes_tuple):
246
+ return encoder(o)
247
+
248
+
249
+ def update_forward_refs(model: Type["Model"], **localns: Any) -> None:
250
+ if IS_PYDANTIC_V2:
251
+ model.model_rebuild(raise_errors=False) # type: ignore[attr-defined]
252
+ else:
253
+ model.update_forward_refs(**localns)
254
+
255
+
256
+ # Mirrors Pydantic's internal typing
257
+ AnyCallable = Callable[..., Any]
258
+
259
+
260
+ def universal_root_validator(
261
+ pre: bool = False,
262
+ ) -> Callable[[AnyCallable], AnyCallable]:
263
+ def decorator(func: AnyCallable) -> AnyCallable:
264
+ if IS_PYDANTIC_V2:
265
+ return cast(
266
+ AnyCallable,
267
+ pydantic.model_validator(mode="before" if pre else "after")(func),
268
+ ) # type: ignore[attr-defined]
269
+ return cast(AnyCallable, pydantic.root_validator(pre=pre)(func)) # type: ignore[call-overload]
270
+
271
+ return decorator
272
+
273
+
274
+ def universal_field_validator(
275
+ field_name: str, pre: bool = False
276
+ ) -> Callable[[AnyCallable], AnyCallable]:
277
+ def decorator(func: AnyCallable) -> AnyCallable:
278
+ if IS_PYDANTIC_V2:
279
+ return cast(
280
+ AnyCallable,
281
+ pydantic.field_validator(field_name, mode="before" if pre else "after")(
282
+ func
283
+ ),
284
+ ) # type: ignore[attr-defined]
285
+ return cast(AnyCallable, pydantic.validator(field_name, pre=pre)(func))
286
+
287
+ return decorator
288
+
289
+
290
+ PydanticField = Union[ModelField, pydantic.fields.FieldInfo]
291
+
292
+
293
+ def _get_model_fields(model: Type["Model"]) -> Mapping[str, PydanticField]:
294
+ if IS_PYDANTIC_V2:
295
+ return cast(Mapping[str, PydanticField], model.model_fields) # type: ignore[attr-defined]
296
+ return cast(Mapping[str, PydanticField], model.__fields__)
297
+
298
+
299
+ def _get_field_default(field: PydanticField) -> Any:
300
+ try:
301
+ value = field.get_default() # type: ignore[union-attr]
302
+ except:
303
+ value = field.default
304
+ if IS_PYDANTIC_V2:
305
+ from pydantic_core import PydanticUndefined
306
+
307
+ if value == PydanticUndefined:
308
+ return None
309
+ return value
310
+ return value
@@ -0,0 +1,60 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ from typing import Any, Dict, List, Optional, Tuple
4
+
5
+ import pydantic
6
+
7
+
8
+ # Flattens dicts to be of the form {"key[subkey][subkey2]": value} where value is not a dict
9
+ def traverse_query_dict(
10
+ dict_flat: Dict[str, Any], key_prefix: Optional[str] = None
11
+ ) -> List[Tuple[str, Any]]:
12
+ result = []
13
+ for k, v in dict_flat.items():
14
+ key = f"{key_prefix}[{k}]" if key_prefix is not None else k
15
+ if isinstance(v, dict):
16
+ result.extend(traverse_query_dict(v, key))
17
+ elif isinstance(v, list):
18
+ for arr_v in v:
19
+ if isinstance(arr_v, dict):
20
+ result.extend(traverse_query_dict(arr_v, key))
21
+ else:
22
+ result.append((key, arr_v))
23
+ else:
24
+ result.append((key, v))
25
+ return result
26
+
27
+
28
+ def single_query_encoder(query_key: str, query_value: Any) -> List[Tuple[str, Any]]:
29
+ if isinstance(query_value, pydantic.BaseModel) or isinstance(query_value, dict):
30
+ if isinstance(query_value, pydantic.BaseModel):
31
+ obj_dict = query_value.dict(by_alias=True)
32
+ else:
33
+ obj_dict = query_value
34
+ return traverse_query_dict(obj_dict, query_key)
35
+ elif isinstance(query_value, list):
36
+ encoded_values: List[Tuple[str, Any]] = []
37
+ for value in query_value:
38
+ if isinstance(value, pydantic.BaseModel) or isinstance(value, dict):
39
+ if isinstance(value, pydantic.BaseModel):
40
+ obj_dict = value.dict(by_alias=True)
41
+ elif isinstance(value, dict):
42
+ obj_dict = value
43
+
44
+ encoded_values.extend(single_query_encoder(query_key, obj_dict))
45
+ else:
46
+ encoded_values.append((query_key, value))
47
+
48
+ return encoded_values
49
+
50
+ return [(query_key, query_value)]
51
+
52
+
53
+ def encode_query(query: Optional[Dict[str, Any]]) -> Optional[List[Tuple[str, Any]]]:
54
+ if query is None:
55
+ return None
56
+
57
+ encoded_query = []
58
+ for k, v in query.items():
59
+ encoded_query.extend(single_query_encoder(k, v))
60
+ return encoded_query
@@ -0,0 +1,11 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ from typing import Any, Dict, Mapping, Optional
4
+
5
+
6
+ def remove_none_from_dict(original: Mapping[str, Optional[Any]]) -> Dict[str, Any]:
7
+ new: Dict[str, Any] = {}
8
+ for key, value in original.items():
9
+ if value is not None:
10
+ new[key] = value
11
+ return new
@@ -0,0 +1,35 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ import typing
4
+
5
+ try:
6
+ from typing import NotRequired # type: ignore
7
+ except ImportError:
8
+ from typing_extensions import NotRequired
9
+
10
+
11
+ class RequestOptions(typing.TypedDict, total=False):
12
+ """
13
+ Additional options for request-specific configuration when calling APIs via the SDK.
14
+ This is used primarily as an optional final parameter for service functions.
15
+
16
+ Attributes:
17
+ - timeout_in_seconds: int. The number of seconds to await an API call before timing out.
18
+
19
+ - max_retries: int. The max number of retries to attempt if the API call fails.
20
+
21
+ - additional_headers: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's header dict
22
+
23
+ - additional_query_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's query parameters dict
24
+
25
+ - additional_body_parameters: typing.Dict[str, typing.Any]. A dictionary containing additional parameters to spread into the request's body parameters dict
26
+
27
+ - chunk_size: int. The size, in bytes, to process each chunk of data being streamed back within the response. This equates to leveraging `chunk_size` within `requests` or `httpx`, and is only leveraged for file downloads.
28
+ """
29
+
30
+ timeout_in_seconds: NotRequired[int]
31
+ max_retries: NotRequired[int]
32
+ additional_headers: NotRequired[typing.Dict[str, typing.Any]]
33
+ additional_query_parameters: NotRequired[typing.Dict[str, typing.Any]]
34
+ additional_body_parameters: NotRequired[typing.Dict[str, typing.Any]]
35
+ chunk_size: NotRequired[int]