agenta 0.24.1a0__py3-none-any.whl → 0.24.2__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.

Potentially problematic release.


This version of agenta might be problematic. Click here for more details.

Files changed (132) hide show
  1. agenta/cli/variant_commands.py +15 -10
  2. agenta/client/Readme.md +72 -64
  3. agenta/client/api.py +1 -1
  4. agenta/client/backend/__init__.py +14 -9
  5. agenta/client/backend/apps/client.py +1669 -0
  6. agenta/client/backend/bases/client.py +190 -0
  7. agenta/client/backend/client.py +2102 -868
  8. agenta/client/backend/configs/client.py +598 -0
  9. agenta/client/backend/containers/client.py +638 -0
  10. agenta/client/backend/{resources/containers → containers}/types/container_templates_response.py +1 -2
  11. agenta/client/backend/core/__init__.py +29 -0
  12. agenta/client/backend/core/client_wrapper.py +42 -9
  13. agenta/client/backend/core/datetime_utils.py +1 -1
  14. agenta/client/backend/core/file.py +43 -0
  15. agenta/client/backend/core/http_client.py +553 -0
  16. agenta/client/backend/core/jsonable_encoder.py +33 -39
  17. agenta/client/backend/core/pydantic_utilities.py +212 -0
  18. agenta/client/backend/core/query_encoder.py +60 -0
  19. agenta/client/backend/core/remove_none_from_dict.py +2 -2
  20. agenta/client/backend/core/request_options.py +32 -0
  21. agenta/client/backend/core/serialization.py +179 -0
  22. agenta/client/backend/environments/client.py +190 -0
  23. agenta/client/backend/evaluations/client.py +1462 -0
  24. agenta/client/backend/evaluators/client.py +911 -0
  25. agenta/client/backend/observability/client.py +1271 -0
  26. agenta/client/backend/testsets/client.py +1132 -0
  27. agenta/client/backend/types/__init__.py +8 -6
  28. agenta/client/backend/types/aggregated_result.py +14 -29
  29. agenta/client/backend/types/aggregated_result_evaluator_config.py +1 -2
  30. agenta/client/backend/types/app.py +13 -28
  31. agenta/client/backend/types/app_variant_response.py +21 -37
  32. agenta/client/backend/types/app_variant_revision.py +17 -32
  33. agenta/client/backend/types/base_output.py +13 -28
  34. agenta/client/backend/types/body_import_testset.py +16 -31
  35. agenta/client/backend/types/config_db.py +16 -31
  36. agenta/client/backend/types/correct_answer.py +22 -0
  37. agenta/client/backend/types/create_app_output.py +13 -28
  38. agenta/client/backend/types/create_span.py +33 -50
  39. agenta/client/backend/types/create_trace_response.py +16 -31
  40. agenta/client/backend/types/docker_env_vars.py +13 -28
  41. agenta/client/backend/types/environment_output.py +21 -36
  42. agenta/client/backend/types/environment_output_extended.py +21 -36
  43. agenta/client/backend/types/environment_revision.py +18 -33
  44. agenta/client/backend/types/error.py +16 -31
  45. agenta/client/backend/types/evaluation.py +20 -34
  46. agenta/client/backend/types/evaluation_scenario.py +18 -33
  47. agenta/client/backend/types/evaluation_scenario_input.py +16 -31
  48. agenta/client/backend/types/evaluation_scenario_output.py +18 -33
  49. agenta/client/backend/types/evaluation_scenario_result.py +14 -29
  50. agenta/client/backend/types/evaluation_scenario_score_update.py +13 -28
  51. agenta/client/backend/types/evaluation_status_enum.py +11 -33
  52. agenta/client/backend/types/evaluation_type.py +3 -21
  53. agenta/client/backend/types/evaluator.py +18 -32
  54. agenta/client/backend/types/evaluator_config.py +20 -33
  55. agenta/client/backend/types/get_config_response.py +16 -31
  56. agenta/client/backend/types/http_validation_error.py +14 -29
  57. agenta/client/backend/types/human_evaluation.py +17 -32
  58. agenta/client/backend/types/human_evaluation_scenario.py +21 -37
  59. agenta/client/backend/types/human_evaluation_scenario_input.py +13 -28
  60. agenta/client/backend/types/human_evaluation_scenario_output.py +13 -28
  61. agenta/client/backend/types/human_evaluation_scenario_update.py +26 -41
  62. agenta/client/backend/types/human_evaluation_update.py +14 -29
  63. agenta/client/backend/types/image.py +18 -33
  64. agenta/client/backend/types/invite_request.py +13 -28
  65. agenta/client/backend/types/list_api_keys_response.py +18 -33
  66. agenta/client/backend/types/llm_run_rate_limit.py +13 -28
  67. agenta/client/backend/types/llm_tokens.py +16 -31
  68. agenta/client/backend/types/lm_providers_enum.py +21 -0
  69. agenta/client/backend/types/new_human_evaluation.py +13 -28
  70. agenta/client/backend/types/new_testset.py +16 -31
  71. agenta/client/backend/types/organization.py +22 -36
  72. agenta/client/backend/types/organization_output.py +13 -28
  73. agenta/client/backend/types/outputs.py +5 -0
  74. agenta/client/backend/types/permission.py +36 -137
  75. agenta/client/backend/types/result.py +17 -32
  76. agenta/client/backend/types/simple_evaluation_output.py +13 -28
  77. agenta/client/backend/types/span.py +23 -38
  78. agenta/client/backend/types/span_detail.py +26 -40
  79. agenta/client/backend/types/span_status_code.py +1 -25
  80. agenta/client/backend/types/span_variant.py +16 -31
  81. agenta/client/backend/types/template.py +14 -29
  82. agenta/client/backend/types/template_image_info.py +21 -35
  83. agenta/client/backend/types/test_set_output_response.py +16 -32
  84. agenta/client/backend/types/test_set_simple_response.py +13 -28
  85. agenta/client/backend/types/trace_detail.py +26 -40
  86. agenta/client/backend/types/update_app_output.py +22 -0
  87. agenta/client/backend/types/uri.py +13 -28
  88. agenta/client/backend/types/validation_error.py +13 -28
  89. agenta/client/backend/types/variant_action.py +14 -29
  90. agenta/client/backend/types/variant_action_enum.py +1 -19
  91. agenta/client/backend/types/with_pagination.py +14 -30
  92. agenta/client/backend/types/workspace_member_response.py +14 -29
  93. agenta/client/backend/types/workspace_permission.py +18 -33
  94. agenta/client/backend/types/workspace_response.py +20 -35
  95. agenta/client/backend/types/workspace_role.py +11 -37
  96. agenta/client/backend/types/workspace_role_response.py +17 -32
  97. agenta/client/backend/variants/client.py +1447 -0
  98. agenta/client/backend/variants/types/add_variant_from_base_and_config_response.py +8 -0
  99. agenta/sdk/decorators/llm_entrypoint.py +8 -13
  100. agenta/sdk/tracing/llm_tracing.py +10 -12
  101. {agenta-0.24.1a0.dist-info → agenta-0.24.2.dist-info}/METADATA +1 -1
  102. agenta-0.24.2.dist-info/RECORD +175 -0
  103. agenta/client/backend/resources/__init__.py +0 -31
  104. agenta/client/backend/resources/apps/client.py +0 -977
  105. agenta/client/backend/resources/bases/client.py +0 -127
  106. agenta/client/backend/resources/configs/client.py +0 -377
  107. agenta/client/backend/resources/containers/client.py +0 -383
  108. agenta/client/backend/resources/environments/client.py +0 -131
  109. agenta/client/backend/resources/evaluations/client.py +0 -1008
  110. agenta/client/backend/resources/evaluators/client.py +0 -594
  111. agenta/client/backend/resources/observability/client.py +0 -1187
  112. agenta/client/backend/resources/testsets/client.py +0 -689
  113. agenta/client/backend/resources/variants/client.py +0 -796
  114. agenta/client/backend/resources/variants/types/add_variant_from_base_and_config_response.py +0 -7
  115. agenta/client/backend/types/evaluation_webhook.py +0 -36
  116. agenta/client/backend/types/feedback.py +0 -40
  117. agenta/client/backend/types/span_kind.py +0 -49
  118. agenta-0.24.1a0.dist-info/RECORD +0 -169
  119. /agenta/client/backend/{resources/apps → apps}/__init__.py +0 -0
  120. /agenta/client/backend/{resources/bases → bases}/__init__.py +0 -0
  121. /agenta/client/backend/{resources/configs → configs}/__init__.py +0 -0
  122. /agenta/client/backend/{resources/containers → containers}/__init__.py +0 -0
  123. /agenta/client/backend/{resources/containers → containers}/types/__init__.py +0 -0
  124. /agenta/client/backend/{resources/environments → environments}/__init__.py +0 -0
  125. /agenta/client/backend/{resources/evaluations → evaluations}/__init__.py +0 -0
  126. /agenta/client/backend/{resources/evaluators → evaluators}/__init__.py +0 -0
  127. /agenta/client/backend/{resources/observability → observability}/__init__.py +0 -0
  128. /agenta/client/backend/{resources/testsets → testsets}/__init__.py +0 -0
  129. /agenta/client/backend/{resources/variants → variants}/__init__.py +0 -0
  130. /agenta/client/backend/{resources/variants → variants}/types/__init__.py +0 -0
  131. {agenta-0.24.1a0.dist-info → agenta-0.24.2.dist-info}/WHEEL +0 -0
  132. {agenta-0.24.1a0.dist-info → agenta-0.24.2.dist-info}/entry_points.txt +0 -0
@@ -8,41 +8,27 @@ Taken from FastAPI, and made a bit simpler
8
8
  https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py
9
9
  """
10
10
 
11
+ import base64
11
12
  import dataclasses
12
13
  import datetime as dt
13
- from collections import defaultdict
14
14
  from enum import Enum
15
15
  from pathlib import PurePath
16
16
  from types import GeneratorType
17
- from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
17
+ from typing import Any, Callable, Dict, List, Optional, Set, Union
18
18
 
19
- try:
20
- import pydantic.v1 as pydantic # type: ignore
21
- except ImportError:
22
- import pydantic # type: ignore
19
+ import pydantic
23
20
 
24
21
  from .datetime_utils import serialize_datetime
22
+ from .pydantic_utilities import (
23
+ IS_PYDANTIC_V2,
24
+ encode_by_type,
25
+ to_jsonable_with_fallback,
26
+ )
25
27
 
26
28
  SetIntStr = Set[Union[int, str]]
27
29
  DictIntStrAny = Dict[Union[int, str], Any]
28
30
 
29
31
 
30
- def generate_encoders_by_class_tuples(
31
- type_encoder_map: Dict[Any, Callable[[Any], Any]]
32
- ) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]:
33
- encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(
34
- tuple
35
- )
36
- for type_, encoder in type_encoder_map.items():
37
- encoders_by_class_tuples[encoder] += (type_,)
38
- return encoders_by_class_tuples
39
-
40
-
41
- encoders_by_class_tuples = generate_encoders_by_class_tuples(
42
- pydantic.json.ENCODERS_BY_TYPE
43
- )
44
-
45
-
46
32
  def jsonable_encoder(
47
33
  obj: Any, custom_encoder: Optional[Dict[Any, Callable[[Any], Any]]] = None
48
34
  ) -> Any:
@@ -55,26 +41,33 @@ def jsonable_encoder(
55
41
  if isinstance(obj, encoder_type):
56
42
  return encoder_instance(obj)
57
43
  if isinstance(obj, pydantic.BaseModel):
58
- encoder = getattr(obj.__config__, "json_encoders", {})
44
+ if IS_PYDANTIC_V2:
45
+ encoder = getattr(obj.model_config, "json_encoders", {}) # type: ignore # Pydantic v2
46
+ else:
47
+ encoder = getattr(obj.__config__, "json_encoders", {}) # type: ignore # Pydantic v1
59
48
  if custom_encoder:
60
49
  encoder.update(custom_encoder)
61
50
  obj_dict = obj.dict(by_alias=True)
62
51
  if "__root__" in obj_dict:
63
52
  obj_dict = obj_dict["__root__"]
53
+ if "root" in obj_dict:
54
+ obj_dict = obj_dict["root"]
64
55
  return jsonable_encoder(obj_dict, custom_encoder=encoder)
65
56
  if dataclasses.is_dataclass(obj):
66
- obj_dict = dataclasses.asdict(obj)
57
+ obj_dict = dataclasses.asdict(obj) # type: ignore
67
58
  return jsonable_encoder(obj_dict, custom_encoder=custom_encoder)
59
+ if isinstance(obj, bytes):
60
+ return base64.b64encode(obj).decode("utf-8")
68
61
  if isinstance(obj, Enum):
69
62
  return obj.value
70
63
  if isinstance(obj, PurePath):
71
64
  return str(obj)
72
65
  if isinstance(obj, (str, int, float, type(None))):
73
66
  return obj
74
- if isinstance(obj, dt.date):
75
- return str(obj)
76
67
  if isinstance(obj, dt.datetime):
77
68
  return serialize_datetime(obj)
69
+ if isinstance(obj, dt.date):
70
+ return str(obj)
78
71
  if isinstance(obj, dict):
79
72
  encoded_dict = {}
80
73
  allowed_keys = set(obj.keys())
@@ -90,20 +83,21 @@ def jsonable_encoder(
90
83
  encoded_list.append(jsonable_encoder(item, custom_encoder=custom_encoder))
91
84
  return encoded_list
92
85
 
93
- if type(obj) in pydantic.json.ENCODERS_BY_TYPE:
94
- return pydantic.json.ENCODERS_BY_TYPE[type(obj)](obj)
95
- for encoder, classes_tuple in encoders_by_class_tuples.items():
96
- if isinstance(obj, classes_tuple):
97
- return encoder(obj)
86
+ def fallback_serializer(o: Any) -> Any:
87
+ attempt_encode = encode_by_type(o)
88
+ if attempt_encode is not None:
89
+ return attempt_encode
98
90
 
99
- try:
100
- data = dict(obj)
101
- except Exception as e:
102
- errors: List[Exception] = []
103
- errors.append(e)
104
91
  try:
105
- data = vars(obj)
92
+ data = dict(o)
106
93
  except Exception as e:
94
+ errors: List[Exception] = []
107
95
  errors.append(e)
108
- raise ValueError(errors) from e
109
- return jsonable_encoder(data, custom_encoder=custom_encoder)
96
+ try:
97
+ data = vars(o)
98
+ except Exception as e:
99
+ errors.append(e)
100
+ raise ValueError(errors) from e
101
+ return jsonable_encoder(data, custom_encoder=custom_encoder)
102
+
103
+ return to_jsonable_with_fallback(obj, fallback_serializer)
@@ -0,0 +1,212 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ # nopycln: file
4
+ import datetime as dt
5
+ import typing
6
+ from collections import defaultdict
7
+
8
+ import typing_extensions
9
+
10
+ import pydantic
11
+
12
+ from .datetime_utils import serialize_datetime
13
+
14
+ IS_PYDANTIC_V2 = pydantic.VERSION.startswith("2.")
15
+
16
+ if IS_PYDANTIC_V2:
17
+ # isort will try to reformat the comments on these imports, which breaks mypy
18
+ # isort: off
19
+ from pydantic.v1.datetime_parse import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
20
+ parse_date as parse_date,
21
+ )
22
+ from pydantic.v1.datetime_parse import ( # pyright: ignore[reportMissingImports] # Pydantic v2
23
+ parse_datetime as parse_datetime,
24
+ )
25
+ from pydantic.v1.json import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
26
+ ENCODERS_BY_TYPE as encoders_by_type,
27
+ )
28
+ from pydantic.v1.typing import ( # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
29
+ get_args as get_args,
30
+ )
31
+ from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
32
+ get_origin as get_origin,
33
+ )
34
+ from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
35
+ is_literal_type as is_literal_type,
36
+ )
37
+ from pydantic.v1.typing import ( # pyright: ignore[reportMissingImports] # Pydantic v2
38
+ is_union as is_union,
39
+ )
40
+ from pydantic.v1.fields import ModelField as ModelField # type: ignore # pyright: ignore[reportMissingImports] # Pydantic v2
41
+ else:
42
+ from pydantic.datetime_parse import parse_date as parse_date # type: ignore # Pydantic v1
43
+ from pydantic.datetime_parse import parse_datetime as parse_datetime # type: ignore # Pydantic v1
44
+ from pydantic.fields import ModelField as ModelField # type: ignore # Pydantic v1
45
+ from pydantic.json import ENCODERS_BY_TYPE as encoders_by_type # type: ignore # Pydantic v1
46
+ from pydantic.typing import get_args as get_args # type: ignore # Pydantic v1
47
+ from pydantic.typing import get_origin as get_origin # type: ignore # Pydantic v1
48
+ from pydantic.typing import is_literal_type as is_literal_type # type: ignore # Pydantic v1
49
+ from pydantic.typing import is_union as is_union # type: ignore # Pydantic v1
50
+
51
+ # isort: on
52
+
53
+
54
+ T = typing.TypeVar("T")
55
+ Model = typing.TypeVar("Model", bound=pydantic.BaseModel)
56
+
57
+
58
+ def parse_obj_as(type_: typing.Type[T], object_: typing.Any) -> T:
59
+ if IS_PYDANTIC_V2:
60
+ adapter = pydantic.TypeAdapter(type_) # type: ignore # Pydantic v2
61
+ return adapter.validate_python(object_)
62
+ else:
63
+ return pydantic.parse_obj_as(type_, object_)
64
+
65
+
66
+ def to_jsonable_with_fallback(
67
+ obj: typing.Any, fallback_serializer: typing.Callable[[typing.Any], typing.Any]
68
+ ) -> typing.Any:
69
+ if IS_PYDANTIC_V2:
70
+ from pydantic_core import to_jsonable_python
71
+
72
+ return to_jsonable_python(obj, fallback=fallback_serializer)
73
+ else:
74
+ return fallback_serializer(obj)
75
+
76
+
77
+ class UniversalBaseModel(pydantic.BaseModel):
78
+ class Config:
79
+ populate_by_name = True
80
+ smart_union = True
81
+ allow_population_by_field_name = True
82
+ json_encoders = {dt.datetime: serialize_datetime}
83
+
84
+ def json(self, **kwargs: typing.Any) -> str:
85
+ kwargs_with_defaults: typing.Any = {
86
+ "by_alias": True,
87
+ "exclude_unset": True,
88
+ **kwargs,
89
+ }
90
+ if IS_PYDANTIC_V2:
91
+ return super().model_dump_json(**kwargs_with_defaults) # type: ignore # Pydantic v2
92
+ else:
93
+ return super().json(**kwargs_with_defaults)
94
+
95
+ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
96
+ """
97
+ Override the default dict method to `exclude_unset` by default. This function patches
98
+ `exclude_unset` to work include fields within non-None default values.
99
+ """
100
+ _fields_set = self.__fields_set__
101
+
102
+ fields = _get_model_fields(self.__class__)
103
+ for name, field in fields.items():
104
+ if name not in _fields_set:
105
+ default = _get_field_default(field)
106
+
107
+ # If the default values are non-null act like they've been set
108
+ # This effectively allows exclude_unset to work like exclude_none where
109
+ # the latter passes through intentionally set none values.
110
+ if default != None:
111
+ _fields_set.add(name)
112
+
113
+ kwargs_with_defaults_exclude_unset: typing.Any = {
114
+ "by_alias": True,
115
+ "exclude_unset": True,
116
+ "include": _fields_set,
117
+ **kwargs,
118
+ }
119
+
120
+ if IS_PYDANTIC_V2:
121
+ return super().model_dump(**kwargs_with_defaults_exclude_unset) # type: ignore # Pydantic v2
122
+ else:
123
+ return super().dict(**kwargs_with_defaults_exclude_unset)
124
+
125
+
126
+ if IS_PYDANTIC_V2:
127
+
128
+ class V2RootModel(UniversalBaseModel, pydantic.RootModel): # type: ignore # Pydantic v2
129
+ pass
130
+
131
+ UniversalRootModel: typing_extensions.TypeAlias = V2RootModel # type: ignore
132
+ else:
133
+ UniversalRootModel: typing_extensions.TypeAlias = UniversalBaseModel # type: ignore
134
+
135
+
136
+ def encode_by_type(o: typing.Any) -> typing.Any:
137
+ encoders_by_class_tuples: typing.Dict[
138
+ typing.Callable[[typing.Any], typing.Any], typing.Tuple[typing.Any, ...]
139
+ ] = defaultdict(tuple)
140
+ for type_, encoder in encoders_by_type.items():
141
+ encoders_by_class_tuples[encoder] += (type_,)
142
+
143
+ if type(o) in encoders_by_type:
144
+ return encoders_by_type[type(o)](o)
145
+ for encoder, classes_tuple in encoders_by_class_tuples.items():
146
+ if isinstance(o, classes_tuple):
147
+ return encoder(o)
148
+
149
+
150
+ def update_forward_refs(model: typing.Type["Model"]) -> None:
151
+ if IS_PYDANTIC_V2:
152
+ model.model_rebuild(raise_errors=False) # type: ignore # Pydantic v2
153
+ else:
154
+ model.update_forward_refs()
155
+
156
+
157
+ # Mirrors Pydantic's internal typing
158
+ AnyCallable = typing.Callable[..., typing.Any]
159
+
160
+
161
+ def universal_root_validator(
162
+ pre: bool = False,
163
+ ) -> typing.Callable[[AnyCallable], AnyCallable]:
164
+ def decorator(func: AnyCallable) -> AnyCallable:
165
+ if IS_PYDANTIC_V2:
166
+ return pydantic.model_validator(mode="before" if pre else "after")(func) # type: ignore # Pydantic v2
167
+ else:
168
+ return pydantic.root_validator(pre=pre)(func) # type: ignore # Pydantic v1
169
+
170
+ return decorator
171
+
172
+
173
+ def universal_field_validator(
174
+ field_name: str, pre: bool = False
175
+ ) -> typing.Callable[[AnyCallable], AnyCallable]:
176
+ def decorator(func: AnyCallable) -> AnyCallable:
177
+ if IS_PYDANTIC_V2:
178
+ return pydantic.field_validator(
179
+ field_name, mode="before" if pre else "after"
180
+ )(
181
+ func
182
+ ) # type: ignore # Pydantic v2
183
+ else:
184
+ return pydantic.validator(field_name, pre=pre)(func) # type: ignore # Pydantic v1
185
+
186
+ return decorator
187
+
188
+
189
+ PydanticField = typing.Union[ModelField, pydantic.fields.FieldInfo]
190
+
191
+
192
+ def _get_model_fields(
193
+ model: typing.Type["Model"],
194
+ ) -> typing.Mapping[str, PydanticField]:
195
+ if IS_PYDANTIC_V2:
196
+ return model.model_fields # type: ignore # Pydantic v2
197
+ else:
198
+ return model.__fields__ # type: ignore # Pydantic v1
199
+
200
+
201
+ def _get_field_default(field: PydanticField) -> typing.Any:
202
+ try:
203
+ value = field.get_default() # type: ignore # Pydantic < v1.10.15
204
+ except:
205
+ value = field.default
206
+ if IS_PYDANTIC_V2:
207
+ from pydantic_core import PydanticUndefined
208
+
209
+ if value == PydanticUndefined:
210
+ return None
211
+ return value
212
+ 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
@@ -1,9 +1,9 @@
1
1
  # This file was auto-generated by Fern from our API Definition.
2
2
 
3
- from typing import Any, Dict, Optional
3
+ from typing import Any, Dict, Mapping, Optional
4
4
 
5
5
 
6
- def remove_none_from_dict(original: Dict[str, Optional[Any]]) -> Dict[str, Any]:
6
+ def remove_none_from_dict(original: Mapping[str, Optional[Any]]) -> Dict[str, Any]:
7
7
  new: Dict[str, Any] = {}
8
8
  for key, value in original.items():
9
9
  if value is not None:
@@ -0,0 +1,32 @@
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
+
28
+ timeout_in_seconds: NotRequired[int]
29
+ max_retries: NotRequired[int]
30
+ additional_headers: NotRequired[typing.Dict[str, typing.Any]]
31
+ additional_query_parameters: NotRequired[typing.Dict[str, typing.Any]]
32
+ additional_body_parameters: NotRequired[typing.Dict[str, typing.Any]]
@@ -0,0 +1,179 @@
1
+ # This file was auto-generated by Fern from our API Definition.
2
+
3
+ import collections
4
+ import typing
5
+
6
+ import typing_extensions
7
+
8
+
9
+ class FieldMetadata:
10
+ """
11
+ Metadata class used to annotate fields to provide additional information.
12
+
13
+ Example:
14
+ class MyDict(TypedDict):
15
+ field: typing.Annotated[str, FieldMetadata(alias="field_name")]
16
+
17
+ Will serialize: `{"field": "value"}`
18
+ To: `{"field_name": "value"}`
19
+ """
20
+
21
+ alias: str
22
+
23
+ def __init__(self, *, alias: str) -> None:
24
+ self.alias = alias
25
+
26
+
27
+ def convert_and_respect_annotation_metadata(
28
+ *,
29
+ object_: typing.Any,
30
+ annotation: typing.Any,
31
+ inner_type: typing.Optional[typing.Any] = None,
32
+ ) -> typing.Any:
33
+ """
34
+ Respect the metadata annotations on a field, such as aliasing. This function effectively
35
+ manipulates the dict-form of an object to respect the metadata annotations. This is primarily used for
36
+ TypedDicts, which cannot support aliasing out of the box, and can be extended for additional
37
+ utilities, such as defaults.
38
+
39
+ Parameters
40
+ ----------
41
+ object_ : typing.Any
42
+
43
+ annotation : type
44
+ The type we're looking to apply typing annotations from
45
+
46
+ inner_type : typing.Optional[type]
47
+
48
+ Returns
49
+ -------
50
+ typing.Any
51
+ """
52
+
53
+ if object_ is None:
54
+ return None
55
+ if inner_type is None:
56
+ inner_type = annotation
57
+
58
+ clean_type = _remove_annotations(inner_type)
59
+ if typing_extensions.is_typeddict(clean_type) and isinstance(
60
+ object_, typing.Mapping
61
+ ):
62
+ return _convert_typeddict(object_, clean_type)
63
+
64
+ if (
65
+ # If you're iterating on a string, do not bother to coerce it to a sequence.
66
+ (not isinstance(object_, str))
67
+ and (
68
+ (
69
+ (
70
+ typing_extensions.get_origin(clean_type) == typing.List
71
+ or typing_extensions.get_origin(clean_type) == list
72
+ or clean_type == typing.List
73
+ )
74
+ and isinstance(object_, typing.List)
75
+ )
76
+ or (
77
+ (
78
+ typing_extensions.get_origin(clean_type) == typing.Set
79
+ or typing_extensions.get_origin(clean_type) == set
80
+ or clean_type == typing.Set
81
+ )
82
+ and isinstance(object_, typing.Set)
83
+ )
84
+ or (
85
+ (
86
+ typing_extensions.get_origin(clean_type) == typing.Sequence
87
+ or typing_extensions.get_origin(clean_type)
88
+ == collections.abc.Sequence
89
+ or clean_type == typing.Sequence
90
+ )
91
+ and isinstance(object_, typing.Sequence)
92
+ )
93
+ )
94
+ ):
95
+ inner_type = typing_extensions.get_args(clean_type)[0]
96
+ return [
97
+ convert_and_respect_annotation_metadata(
98
+ object_=item, annotation=annotation, inner_type=inner_type
99
+ )
100
+ for item in object_
101
+ ]
102
+
103
+ if typing_extensions.get_origin(clean_type) == typing.Union:
104
+ # We should be able to ~relatively~ safely try to convert keys against all
105
+ # member types in the union, the edge case here is if one member aliases a field
106
+ # of the same name to a different name from another member
107
+ # Or if another member aliases a field of the same name that another member does not.
108
+ for member in typing_extensions.get_args(clean_type):
109
+ object_ = convert_and_respect_annotation_metadata(
110
+ object_=object_, annotation=annotation, inner_type=member
111
+ )
112
+ return object_
113
+
114
+ annotated_type = _get_annotation(annotation)
115
+ if annotated_type is None:
116
+ return object_
117
+
118
+ # If the object is not a TypedDict, a Union, or other container (list, set, sequence, etc.)
119
+ # Then we can safely call it on the recursive conversion.
120
+ return object_
121
+
122
+
123
+ def _convert_typeddict(
124
+ object_: typing.Mapping[str, object], expected_type: typing.Any
125
+ ) -> typing.Mapping[str, object]:
126
+ converted_object: typing.Dict[str, object] = {}
127
+ annotations = typing_extensions.get_type_hints(expected_type, include_extras=True)
128
+ for key, value in object_.items():
129
+ type_ = annotations.get(key)
130
+ if type_ is None:
131
+ converted_object[key] = value
132
+ else:
133
+ converted_object[
134
+ _alias_key(key, type_)
135
+ ] = convert_and_respect_annotation_metadata(object_=value, annotation=type_)
136
+ return converted_object
137
+
138
+
139
+ def _get_annotation(type_: typing.Any) -> typing.Optional[typing.Any]:
140
+ maybe_annotated_type = typing_extensions.get_origin(type_)
141
+ if maybe_annotated_type is None:
142
+ return None
143
+
144
+ if maybe_annotated_type == typing_extensions.NotRequired:
145
+ type_ = typing_extensions.get_args(type_)[0]
146
+ maybe_annotated_type = typing_extensions.get_origin(type_)
147
+
148
+ if maybe_annotated_type == typing_extensions.Annotated:
149
+ return type_
150
+
151
+ return None
152
+
153
+
154
+ def _remove_annotations(type_: typing.Any) -> typing.Any:
155
+ maybe_annotated_type = typing_extensions.get_origin(type_)
156
+ if maybe_annotated_type is None:
157
+ return type_
158
+
159
+ if maybe_annotated_type == typing_extensions.NotRequired:
160
+ return _remove_annotations(typing_extensions.get_args(type_)[0])
161
+
162
+ if maybe_annotated_type == typing_extensions.Annotated:
163
+ return _remove_annotations(typing_extensions.get_args(type_)[0])
164
+
165
+ return type_
166
+
167
+
168
+ def _alias_key(key: str, type_: typing.Any) -> str:
169
+ maybe_annotated_type = _get_annotation(type_)
170
+
171
+ if maybe_annotated_type is not None:
172
+ # The actual annotations are 1 onward, the first is the annotated type
173
+ annotations = typing_extensions.get_args(maybe_annotated_type)[1:]
174
+
175
+ for annotation in annotations:
176
+ if isinstance(annotation, FieldMetadata) and annotation.alias is not None:
177
+ return annotation.alias
178
+
179
+ return key