mirascope 2.0.0a1__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 (205) 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/decorator.py +10 -10
  136. mirascope/llm/tools/protocols.py +4 -4
  137. mirascope/llm/tools/tool_schema.py +44 -9
  138. mirascope/llm/tools/tools.py +12 -11
  139. mirascope/ops/__init__.py +156 -0
  140. mirascope/ops/_internal/__init__.py +5 -0
  141. mirascope/ops/_internal/closure.py +1118 -0
  142. mirascope/ops/_internal/configuration.py +126 -0
  143. mirascope/ops/_internal/context.py +76 -0
  144. mirascope/ops/_internal/exporters/__init__.py +26 -0
  145. mirascope/ops/_internal/exporters/exporters.py +342 -0
  146. mirascope/ops/_internal/exporters/processors.py +104 -0
  147. mirascope/ops/_internal/exporters/types.py +165 -0
  148. mirascope/ops/_internal/exporters/utils.py +29 -0
  149. mirascope/ops/_internal/instrumentation/__init__.py +8 -0
  150. mirascope/ops/_internal/instrumentation/llm/__init__.py +8 -0
  151. mirascope/ops/_internal/instrumentation/llm/encode.py +238 -0
  152. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/__init__.py +38 -0
  153. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_input_messages.py +31 -0
  154. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_output_messages.py +38 -0
  155. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/gen_ai_system_instructions.py +18 -0
  156. mirascope/ops/_internal/instrumentation/llm/gen_ai_types/shared.py +100 -0
  157. mirascope/ops/_internal/instrumentation/llm/llm.py +1288 -0
  158. mirascope/ops/_internal/propagation.py +198 -0
  159. mirascope/ops/_internal/protocols.py +51 -0
  160. mirascope/ops/_internal/session.py +139 -0
  161. mirascope/ops/_internal/spans.py +232 -0
  162. mirascope/ops/_internal/traced_calls.py +371 -0
  163. mirascope/ops/_internal/traced_functions.py +394 -0
  164. mirascope/ops/_internal/tracing.py +276 -0
  165. mirascope/ops/_internal/types.py +13 -0
  166. mirascope/ops/_internal/utils.py +75 -0
  167. mirascope/ops/_internal/versioned_calls.py +512 -0
  168. mirascope/ops/_internal/versioned_functions.py +346 -0
  169. mirascope/ops/_internal/versioning.py +303 -0
  170. mirascope/ops/exceptions.py +21 -0
  171. {mirascope-2.0.0a1.dist-info → mirascope-2.0.0a3.dist-info}/METADATA +77 -1
  172. mirascope-2.0.0a3.dist-info/RECORD +206 -0
  173. {mirascope-2.0.0a1.dist-info → mirascope-2.0.0a3.dist-info}/WHEEL +1 -1
  174. mirascope/graphs/__init__.py +0 -22
  175. mirascope/graphs/finite_state_machine.py +0 -625
  176. mirascope/llm/agents/__init__.py +0 -15
  177. mirascope/llm/agents/agent.py +0 -97
  178. mirascope/llm/agents/agent_template.py +0 -45
  179. mirascope/llm/agents/decorator.py +0 -176
  180. mirascope/llm/calls/base_call.py +0 -33
  181. mirascope/llm/clients/__init__.py +0 -34
  182. mirascope/llm/clients/anthropic/__init__.py +0 -25
  183. mirascope/llm/clients/anthropic/model_ids.py +0 -8
  184. mirascope/llm/clients/google/__init__.py +0 -20
  185. mirascope/llm/clients/google/clients.py +0 -853
  186. mirascope/llm/clients/google/model_ids.py +0 -15
  187. mirascope/llm/clients/openai/__init__.py +0 -25
  188. mirascope/llm/clients/openai/completions/__init__.py +0 -28
  189. mirascope/llm/clients/openai/completions/_utils/model_features.py +0 -81
  190. mirascope/llm/clients/openai/completions/clients.py +0 -833
  191. mirascope/llm/clients/openai/completions/model_ids.py +0 -8
  192. mirascope/llm/clients/openai/responses/__init__.py +0 -26
  193. mirascope/llm/clients/openai/responses/_utils/model_features.py +0 -87
  194. mirascope/llm/clients/openai/responses/clients.py +0 -832
  195. mirascope/llm/clients/openai/responses/model_ids.py +0 -8
  196. mirascope/llm/clients/providers.py +0 -175
  197. mirascope-2.0.0a1.dist-info/RECORD +0 -102
  198. /mirascope/llm/{clients → providers}/anthropic/_utils/__init__.py +0 -0
  199. /mirascope/llm/{clients → providers}/base/kwargs.py +0 -0
  200. /mirascope/llm/{clients → providers}/base/params.py +0 -0
  201. /mirascope/llm/{clients → providers}/google/_utils/__init__.py +0 -0
  202. /mirascope/llm/{clients → providers}/google/message.py +0 -0
  203. /mirascope/llm/{clients/openai/completions → providers/openai/responses}/_utils/__init__.py +0 -0
  204. /mirascope/llm/{clients → providers}/openai/shared/__init__.py +0 -0
  205. {mirascope-2.0.0a1.dist-info → mirascope-2.0.0a3.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,75 @@
1
+ """Internal utility functions for the Mirascope."""
2
+
3
+ import inspect
4
+ from collections.abc import Callable
5
+ from typing import Any, TypeAlias
6
+
7
+ import orjson
8
+
9
+ from .protocols import P
10
+
11
+ ORJSON_OPTS = (
12
+ orjson.OPT_NON_STR_KEYS
13
+ | orjson.OPT_NAIVE_UTC
14
+ | orjson.OPT_SERIALIZE_NUMPY
15
+ | orjson.OPT_SERIALIZE_DATACLASS
16
+ | orjson.OPT_SERIALIZE_UUID
17
+ )
18
+
19
+ PrimitiveType: TypeAlias = str | int | float | bool
20
+
21
+
22
+ def json_dumps(obj: Any) -> str: # noqa: ANN401
23
+ """Serialize Python objects to JSON using orjson."""
24
+ # json should be utf-8 encoded, json key should be str
25
+ return orjson.dumps(obj, option=ORJSON_OPTS).decode("utf-8")
26
+
27
+
28
+ def get_qualified_name(fn: Callable[..., Any]) -> str:
29
+ """Return the simplified qualified name of a function.
30
+
31
+ If the function is defined locally, return the name after '<locals>.'; otherwise,
32
+ return the last non-empty part after splitting by '.'.
33
+ """
34
+ qualified_name = fn.__qualname__
35
+ if "<locals>." in qualified_name:
36
+ return qualified_name.split("<locals>.")[-1]
37
+ else:
38
+ parts = [part for part in qualified_name.split(".") if part]
39
+ return parts[-1] if parts else qualified_name
40
+
41
+
42
+ def extract_arguments(
43
+ fn: Callable[P, Any],
44
+ *args: P.args,
45
+ **kwargs: P.kwargs,
46
+ ) -> tuple[dict[str, str], dict[str, Any]]:
47
+ """Returns a tuple of (arg_types, arg_values) dictionaries from function call."""
48
+ signature = inspect.signature(fn)
49
+ bound_arguments = signature.bind(*args, **kwargs)
50
+ bound_arguments.apply_defaults()
51
+
52
+ arg_types: dict[str, str] = {}
53
+ arg_values: dict[str, Any] = {}
54
+
55
+ for param_name, param_value in bound_arguments.arguments.items():
56
+ parameter = signature.parameters[param_name]
57
+ if parameter.annotation != inspect.Parameter.empty:
58
+ if hasattr(parameter.annotation, "__name__"):
59
+ type_str = parameter.annotation.__name__
60
+ else:
61
+ type_str = str(parameter.annotation)
62
+ arg_types[param_name] = type_str
63
+ else:
64
+ arg_types[param_name] = type(param_value).__name__
65
+
66
+ if isinstance(param_value, PrimitiveType) or param_value is None:
67
+ arg_values[param_name] = param_value
68
+ else:
69
+ try:
70
+ json_dumps(param_value)
71
+ arg_values[param_name] = param_value
72
+ except (TypeError, ValueError):
73
+ arg_values[param_name] = repr(param_value)
74
+
75
+ return arg_types, arg_values
@@ -0,0 +1,512 @@
1
+ """Versioned call wrappers for @ops.version decorated @llm.call functions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from dataclasses import dataclass, field
7
+ from typing import Concatenate, Generic
8
+ from typing_extensions import TypeIs
9
+
10
+ from ...llm.calls import AsyncCall, AsyncContextCall, Call, ContextCall
11
+ from ...llm.context import Context, DepsT
12
+ from ...llm.formatting import FormattableT
13
+ from ...llm.responses import (
14
+ AsyncContextResponse,
15
+ AsyncContextStreamResponse,
16
+ AsyncResponse,
17
+ AsyncStreamResponse,
18
+ ContextResponse,
19
+ ContextStreamResponse,
20
+ Response,
21
+ StreamResponse,
22
+ )
23
+ from ...llm.types import P
24
+ from ..exceptions import ClosureComputationError
25
+ from .closure import Closure
26
+ from .protocols import (
27
+ AsyncFunction,
28
+ R,
29
+ SyncFunction,
30
+ )
31
+ from .versioned_functions import (
32
+ AsyncVersionedFunction,
33
+ AsyncVersionedResult,
34
+ VersionedFunction,
35
+ VersionedResult,
36
+ VersionInfo,
37
+ _BaseVersionedFunction,
38
+ )
39
+
40
+ logger = logging.getLogger(__name__)
41
+
42
+
43
+ def _compute_closure_from_call(
44
+ call_obj: (
45
+ ContextCall[P, DepsT, FormattableT]
46
+ | AsyncContextCall[P, DepsT, FormattableT]
47
+ | Call[P, FormattableT]
48
+ | AsyncCall[P, FormattableT]
49
+ ),
50
+ ) -> Closure | None:
51
+ """Compute closure from a Call object's prompt function.
52
+
53
+ Extracts the original function from the Call's prompt closure and computes
54
+ a Closure object for version tracking. Returns None if closure computation
55
+ fails.
56
+ """
57
+ fn = call_obj.prompt.fn
58
+ closure = getattr(fn, "__closure__", None)
59
+ original_fn = closure[0].cell_contents if closure and len(closure) > 0 else fn
60
+ try:
61
+ return Closure.from_fn(original_fn)
62
+ except ClosureComputationError as e:
63
+ logger.warning(
64
+ "Failed to build closure for %s; continuing without version registration: %s",
65
+ e.qualified_name,
66
+ e,
67
+ )
68
+ return None
69
+
70
+
71
+ def is_version_call_type(
72
+ fn: (
73
+ SyncFunction[P, R]
74
+ | AsyncFunction[P, R]
75
+ | ContextCall[P, DepsT, FormattableT]
76
+ | AsyncContextCall[P, DepsT, FormattableT]
77
+ | Call[P, FormattableT]
78
+ | AsyncCall[P, FormattableT]
79
+ ),
80
+ ) -> TypeIs[
81
+ ContextCall[P, DepsT, FormattableT]
82
+ | AsyncContextCall[P, DepsT, FormattableT]
83
+ | Call[P, FormattableT]
84
+ | AsyncCall[P, FormattableT]
85
+ ]:
86
+ """Check if fn is any of the Call types."""
87
+ return isinstance(fn, Call | AsyncCall | ContextCall | AsyncContextCall)
88
+
89
+
90
+ def wrap_version_call(
91
+ fn: (
92
+ ContextCall[P, DepsT, FormattableT]
93
+ | AsyncContextCall[P, DepsT, FormattableT]
94
+ | Call[P, FormattableT]
95
+ | AsyncCall[P, FormattableT]
96
+ ),
97
+ tags: tuple[str, ...],
98
+ name: str | None,
99
+ metadata: dict[str, str],
100
+ ) -> (
101
+ VersionedContextCall[P, DepsT, FormattableT]
102
+ | VersionedAsyncContextCall[P, DepsT, FormattableT]
103
+ | VersionedCall[P, FormattableT]
104
+ | VersionedAsyncCall[P, FormattableT]
105
+ ):
106
+ """Wrap a Call object with the appropriate VersionedCall type."""
107
+ if isinstance(fn, AsyncContextCall):
108
+ return VersionedAsyncContextCall(
109
+ _call=fn, tags=tags, name=name, metadata=metadata
110
+ )
111
+ elif isinstance(fn, ContextCall):
112
+ return VersionedContextCall(_call=fn, tags=tags, name=name, metadata=metadata)
113
+ elif isinstance(fn, AsyncCall):
114
+ return VersionedAsyncCall(_call=fn, tags=tags, name=name, metadata=metadata)
115
+ else:
116
+ return VersionedCall(_call=fn, tags=tags, name=name, metadata=metadata)
117
+
118
+
119
+ @dataclass(kw_only=True)
120
+ class _BaseVersionedCall:
121
+ """Base class for versioned call wrappers.
122
+
123
+ Provides common fields and methods for all versioned call types,
124
+ reducing code duplication across VersionedCall, VersionedAsyncCall,
125
+ VersionedContextCall, and VersionedAsyncContextCall.
126
+ """
127
+
128
+ tags: tuple[str, ...]
129
+ """Tags to be associated with versioned calls."""
130
+
131
+ name: str | None = None
132
+ """Optional custom name for the versioned function."""
133
+
134
+ metadata: dict[str, str] = field(default_factory=dict)
135
+ """Arbitrary key-value pairs for additional metadata."""
136
+
137
+ closure: Closure | None = field(init=False, default=None)
138
+ """Cached closure for the original function."""
139
+
140
+ def _compute_closure(
141
+ self,
142
+ wrapped_call: (
143
+ ContextCall[P, DepsT, FormattableT]
144
+ | AsyncContextCall[P, DepsT, FormattableT]
145
+ | Call[P, FormattableT]
146
+ | AsyncCall[P, FormattableT]
147
+ ),
148
+ versioned_call: _BaseVersionedFunction[..., object],
149
+ versioned_stream: _BaseVersionedFunction[..., object],
150
+ ) -> None:
151
+ """Initialize closure from wrapped call and assign to versioned wrappers."""
152
+ self.closure = _compute_closure_from_call(wrapped_call)
153
+ if self.closure is not None:
154
+ versioned_call.closure = self.closure
155
+ versioned_stream.closure = self.closure
156
+
157
+ @property
158
+ def version_info(self) -> VersionInfo | None:
159
+ """Returns static version metadata for this versioned call."""
160
+ if self.closure is None:
161
+ return None
162
+ return VersionInfo(
163
+ uuid=None,
164
+ hash=self.closure.hash,
165
+ signature_hash=self.closure.signature_hash,
166
+ name=self.name or self.closure.name,
167
+ description=self.closure.docstring,
168
+ version=_BaseVersionedFunction._compute_version(self.closure.hash),
169
+ tags=self.tags,
170
+ metadata=self.metadata,
171
+ )
172
+
173
+
174
+ @dataclass(kw_only=True)
175
+ class VersionedCall(_BaseVersionedCall, Generic[P, FormattableT]):
176
+ """Versioned wrapper for synchronous Call objects.
177
+
178
+ When @ops.version decorates an @llm.call, it returns a VersionedCall that wraps
179
+ the call and stream methods with versioning capabilities.
180
+
181
+ Example:
182
+ ```python
183
+ @ops.version
184
+ @llm.call("gpt-4o-mini")
185
+ def recommend_book(genre: str):
186
+ return f"Recommend a {genre} book"
187
+
188
+ # Returns Response directly (but execution is versioned)
189
+ response = recommend_book("fantasy")
190
+ print(response.content)
191
+
192
+ # Same as __call__
193
+ response = recommend_book.call("fantasy")
194
+
195
+ # Streaming returns StreamResponse (versioned)
196
+ stream = recommend_book.stream("fantasy")
197
+ for chunk in stream:
198
+ print(chunk)
199
+
200
+ # Use wrapped to get VersionedResult[Response] with version info
201
+ result = recommend_book.wrapped("fantasy")
202
+ print(result.result.content)
203
+ print(result.function_uuid)
204
+
205
+ # Use wrapped_stream to get VersionedResult[StreamResponse]
206
+ result = recommend_book.wrapped_stream("fantasy")
207
+
208
+ # Access version info
209
+ info = recommend_book.version_info
210
+ print(info.version, info.hash)
211
+ ```
212
+ """
213
+
214
+ _call: Call[P, FormattableT]
215
+ """The wrapped Call object."""
216
+
217
+ call: VersionedFunction[P, Response | Response[FormattableT]] = field(init=False)
218
+ """VersionedFunction wrapping the call method."""
219
+
220
+ stream: VersionedFunction[P, StreamResponse | StreamResponse[FormattableT]] = field(
221
+ init=False
222
+ )
223
+ """VersionedFunction wrapping the stream method."""
224
+
225
+ def __post_init__(self) -> None:
226
+ """Initialize VersionedFunction wrappers for call and stream methods."""
227
+ self.call = VersionedFunction(
228
+ fn=self._call.call,
229
+ tags=self.tags,
230
+ name=self.name,
231
+ metadata=self.metadata,
232
+ )
233
+ self.stream = VersionedFunction(
234
+ fn=self._call.stream,
235
+ tags=self.tags,
236
+ name=self.name,
237
+ metadata=self.metadata,
238
+ )
239
+ self._compute_closure(self._call, self.call, self.stream)
240
+
241
+ def __call__(
242
+ self, *args: P.args, **kwargs: P.kwargs
243
+ ) -> Response | Response[FormattableT]:
244
+ """Call the versioned function and return Response directly."""
245
+ return self.call(*args, **kwargs)
246
+
247
+ def wrapped(
248
+ self, *args: P.args, **kwargs: P.kwargs
249
+ ) -> VersionedResult[Response | Response[FormattableT]]:
250
+ """Call the versioned function and return a wrapped Response."""
251
+ return self.call.wrapped(*args, **kwargs)
252
+
253
+ def wrapped_stream(
254
+ self, *args: P.args, **kwargs: P.kwargs
255
+ ) -> VersionedResult[StreamResponse | StreamResponse[FormattableT]]:
256
+ """Stream the versioned function and return a wrapped StreamResponse."""
257
+ return self.stream.wrapped(*args, **kwargs)
258
+
259
+
260
+ @dataclass(kw_only=True)
261
+ class VersionedAsyncCall(_BaseVersionedCall, Generic[P, FormattableT]):
262
+ """Versioned wrapper for asynchronous AsyncCall objects.
263
+
264
+ Example:
265
+ ```python
266
+ @ops.version
267
+ @llm.call("gpt-4o-mini")
268
+ async def recommend_book(genre: str):
269
+ return f"Recommend a {genre} book"
270
+
271
+ # Returns AsyncResponse directly (but execution is versioned)
272
+ response = await recommend_book("fantasy")
273
+ print(response.content)
274
+
275
+ # Use wrapped to get AsyncVersionedResult[AsyncResponse] with version info
276
+ result = await recommend_book.wrapped("fantasy")
277
+ print(result.result.content)
278
+ print(result.function_uuid)
279
+ ```
280
+ """
281
+
282
+ _call: AsyncCall[P, FormattableT]
283
+ """The wrapped AsyncCall object."""
284
+
285
+ call: AsyncVersionedFunction[P, AsyncResponse | AsyncResponse[FormattableT]] = (
286
+ field(init=False)
287
+ )
288
+ """AsyncVersionedFunction wrapping the call method."""
289
+
290
+ stream: AsyncVersionedFunction[
291
+ P, AsyncStreamResponse | AsyncStreamResponse[FormattableT]
292
+ ] = field(init=False)
293
+ """AsyncVersionedFunction wrapping the stream method."""
294
+
295
+ def __post_init__(self) -> None:
296
+ """Initialize AsyncVersionedFunction wrappers for call and stream methods."""
297
+ self.call = AsyncVersionedFunction(
298
+ fn=self._call.call,
299
+ tags=self.tags,
300
+ name=self.name,
301
+ metadata=self.metadata,
302
+ )
303
+ self.stream = AsyncVersionedFunction(
304
+ fn=self._call.stream,
305
+ tags=self.tags,
306
+ name=self.name,
307
+ metadata=self.metadata,
308
+ )
309
+ self._compute_closure(self._call, self.call, self.stream)
310
+
311
+ async def __call__(
312
+ self, *args: P.args, **kwargs: P.kwargs
313
+ ) -> AsyncResponse | AsyncResponse[FormattableT]:
314
+ """Call the versioned function and return AsyncResponse directly."""
315
+ return await self.call(*args, **kwargs)
316
+
317
+ async def wrapped(
318
+ self, *args: P.args, **kwargs: P.kwargs
319
+ ) -> AsyncVersionedResult[AsyncResponse | AsyncResponse[FormattableT]]:
320
+ """Call the versioned function and return a wrapped Response."""
321
+ return await self.call.wrapped(*args, **kwargs)
322
+
323
+ async def wrapped_stream(
324
+ self, *args: P.args, **kwargs: P.kwargs
325
+ ) -> AsyncVersionedResult[AsyncStreamResponse | AsyncStreamResponse[FormattableT]]:
326
+ """Stream the versioned function and return a wrapped StreamResponse."""
327
+ return await self.stream.wrapped(*args, **kwargs)
328
+
329
+
330
+ @dataclass(kw_only=True)
331
+ class VersionedContextCall(_BaseVersionedCall, Generic[P, DepsT, FormattableT]):
332
+ """Versioned wrapper for synchronous ContextCall objects.
333
+
334
+ Example:
335
+ ```python
336
+ @ops.version
337
+ @llm.call("gpt-4o-mini")
338
+ def recommend_book(ctx: llm.Context[str], genre: str):
339
+ return f"{ctx.deps} Recommend a {genre} book"
340
+
341
+ ctx = llm.Context(deps="As a librarian,")
342
+
343
+ # Returns ContextResponse directly (but execution is versioned)
344
+ response = recommend_book(ctx, "fantasy")
345
+ print(response.content)
346
+
347
+ # Use wrapped to get VersionedResult[ContextResponse] with version info
348
+ result = recommend_book.wrapped(ctx, "fantasy")
349
+ print(result.result.content)
350
+ ```
351
+ """
352
+
353
+ _call: ContextCall[P, DepsT, FormattableT]
354
+ """The wrapped ContextCall object."""
355
+
356
+ _call_versioned: VersionedFunction[
357
+ Concatenate[Context[DepsT], P],
358
+ ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT],
359
+ ] = field(init=False)
360
+ """VersionedFunction wrapping the call method."""
361
+
362
+ _stream_versioned: VersionedFunction[
363
+ Concatenate[Context[DepsT], P],
364
+ ContextStreamResponse[DepsT, None] | ContextStreamResponse[DepsT, FormattableT],
365
+ ] = field(init=False)
366
+ """VersionedFunction wrapping the stream method."""
367
+
368
+ def __post_init__(self) -> None:
369
+ """Initialize VersionedFunction wrappers for call and stream methods."""
370
+ self._call_versioned = VersionedFunction(
371
+ fn=self._call.call,
372
+ tags=self.tags,
373
+ name=self.name,
374
+ metadata=self.metadata,
375
+ )
376
+ self._stream_versioned = VersionedFunction(
377
+ fn=self._call.stream,
378
+ tags=self.tags,
379
+ name=self.name,
380
+ metadata=self.metadata,
381
+ )
382
+ self._compute_closure(self._call, self._call_versioned, self._stream_versioned)
383
+
384
+ def call(
385
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
386
+ ) -> ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]:
387
+ """Call the versioned function and return ContextResponse directly."""
388
+ return self._call_versioned(ctx, *args, **kwargs)
389
+
390
+ def __call__(
391
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
392
+ ) -> ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]:
393
+ """Call the versioned function and return ContextResponse directly."""
394
+ return self.call(ctx, *args, **kwargs)
395
+
396
+ def stream(
397
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
398
+ ) -> (
399
+ ContextStreamResponse[DepsT, None] | ContextStreamResponse[DepsT, FormattableT]
400
+ ):
401
+ """Stream the versioned function and return a ContextStreamResponse."""
402
+ return self._stream_versioned(ctx, *args, **kwargs)
403
+
404
+ def wrapped(
405
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
406
+ ) -> VersionedResult[
407
+ ContextResponse[DepsT, None] | ContextResponse[DepsT, FormattableT]
408
+ ]:
409
+ """Call the versioned function and return a wrapped Response."""
410
+ return self._call_versioned.wrapped(ctx, *args, **kwargs)
411
+
412
+ def wrapped_stream(
413
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
414
+ ) -> VersionedResult[
415
+ ContextStreamResponse[DepsT, None] | ContextStreamResponse[DepsT, FormattableT]
416
+ ]:
417
+ """Stream the versioned function and return a wrapped StreamResponse."""
418
+ return self._stream_versioned.wrapped(ctx, *args, **kwargs)
419
+
420
+
421
+ @dataclass(kw_only=True)
422
+ class VersionedAsyncContextCall(_BaseVersionedCall, Generic[P, DepsT, FormattableT]):
423
+ """Versioned wrapper for asynchronous AsyncContextCall objects.
424
+
425
+ Example:
426
+ ```python
427
+ @ops.version
428
+ @llm.call("gpt-4o-mini")
429
+ async def recommend_book(ctx: llm.Context[str], genre: str):
430
+ return f"{ctx.deps} Recommend a {genre} book"
431
+
432
+ ctx = llm.Context(deps="As a librarian,")
433
+
434
+ # Returns AsyncContextResponse directly (but execution is versioned)
435
+ response = await recommend_book(ctx, "fantasy")
436
+ print(response.content)
437
+
438
+ # Use wrapped to get AsyncVersionedResult[AsyncContextResponse] with version info
439
+ result = await recommend_book.wrapped(ctx, "fantasy")
440
+ print(result.result.content)
441
+ ```
442
+ """
443
+
444
+ _call: AsyncContextCall[P, DepsT, FormattableT]
445
+ """The wrapped AsyncContextCall object."""
446
+
447
+ _call_versioned: AsyncVersionedFunction[
448
+ Concatenate[Context[DepsT], P],
449
+ AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT],
450
+ ] = field(init=False)
451
+ """AsyncVersionedFunction wrapping the call method."""
452
+
453
+ _stream_versioned: AsyncVersionedFunction[
454
+ Concatenate[Context[DepsT], P],
455
+ AsyncContextStreamResponse[DepsT, None]
456
+ | AsyncContextStreamResponse[DepsT, FormattableT],
457
+ ] = field(init=False)
458
+ """AsyncVersionedFunction wrapping the stream method."""
459
+
460
+ def __post_init__(self) -> None:
461
+ """Initialize AsyncVersionedFunction wrappers for call and stream methods."""
462
+ self._call_versioned = AsyncVersionedFunction(
463
+ fn=self._call.call,
464
+ tags=self.tags,
465
+ name=self.name,
466
+ metadata=self.metadata,
467
+ )
468
+ self._stream_versioned = AsyncVersionedFunction(
469
+ fn=self._call.stream,
470
+ tags=self.tags,
471
+ name=self.name,
472
+ metadata=self.metadata,
473
+ )
474
+ self._compute_closure(self._call, self._call_versioned, self._stream_versioned)
475
+
476
+ async def call(
477
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
478
+ ) -> AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]:
479
+ """Call the versioned function and return AsyncContextResponse directly."""
480
+ return await self._call_versioned(ctx, *args, **kwargs)
481
+
482
+ async def __call__(
483
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
484
+ ) -> AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]:
485
+ """Call the versioned function and return AsyncContextResponse directly."""
486
+ return await self.call(ctx, *args, **kwargs)
487
+
488
+ async def stream(
489
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
490
+ ) -> (
491
+ AsyncContextStreamResponse[DepsT, None]
492
+ | AsyncContextStreamResponse[DepsT, FormattableT]
493
+ ):
494
+ """Stream the versioned function and return an AsyncContextStreamResponse."""
495
+ return await self._stream_versioned(ctx, *args, **kwargs)
496
+
497
+ async def wrapped(
498
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
499
+ ) -> AsyncVersionedResult[
500
+ AsyncContextResponse[DepsT, None] | AsyncContextResponse[DepsT, FormattableT]
501
+ ]:
502
+ """Call the versioned function and return a wrapped Response."""
503
+ return await self._call_versioned.wrapped(ctx, *args, **kwargs)
504
+
505
+ async def wrapped_stream(
506
+ self, ctx: Context[DepsT], *args: P.args, **kwargs: P.kwargs
507
+ ) -> AsyncVersionedResult[
508
+ AsyncContextStreamResponse[DepsT, None]
509
+ | AsyncContextStreamResponse[DepsT, FormattableT]
510
+ ]:
511
+ """Stream the versioned function and return a wrapped StreamResponse."""
512
+ return await self._stream_versioned.wrapped(ctx, *args, **kwargs)