mirascope 1.21.3__py3-none-any.whl → 1.21.4__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.
mirascope/llm/_call.py CHANGED
@@ -20,7 +20,11 @@ from ..core.base import (
20
20
  from ..core.base._utils import fn_is_async
21
21
  from ..core.base.stream_config import StreamConfig
22
22
  from ..core.base.types import LocalProvider, Provider
23
- from ._context import CallArgs, apply_context_overrides_to_call_args
23
+ from ._context import (
24
+ CallArgs,
25
+ apply_context_overrides_to_call_args,
26
+ get_current_context,
27
+ )
24
28
  from ._protocols import (
25
29
  AsyncLLMFunctionDecorator,
26
30
  CallDecorator,
@@ -235,55 +239,66 @@ def _call(
235
239
  | Awaitable[(_ResponseModelT | CallResponse)],
236
240
  ]:
237
241
  if fn_is_async(fn):
238
-
242
+ # Create a wrapper function that captures the current context when called
239
243
  @wraps(fn)
240
- async def inner_async(
244
+ def wrapper_with_context(
241
245
  *args: _P.args, **kwargs: _P.kwargs
242
- ) -> (
246
+ ) -> Awaitable[
243
247
  CallResponse
244
248
  | Stream
245
249
  | _ResponseModelT
246
250
  | _ParsedOutputT
247
251
  | (_ResponseModelT | CallResponse)
248
- ):
249
- # Apply any context overrides to the original call args
250
- effective_call_args = apply_context_overrides_to_call_args(
251
- original_call_args
252
- )
252
+ ]:
253
+ # Capture the context at call time
254
+ current_context = get_current_context()
255
+
256
+ # Define an async function that uses the captured context
257
+ async def context_bound_inner_async() -> (
258
+ CallResponse
259
+ | Stream
260
+ | _ResponseModelT
261
+ | _ParsedOutputT
262
+ | (_ResponseModelT | CallResponse)
263
+ ):
264
+ # Apply any context overrides to the original call args
265
+ effective_call_args = apply_context_overrides_to_call_args(
266
+ original_call_args, context_override=current_context
267
+ )
253
268
 
254
- # Get the appropriate provider call function with the possibly overridden provider
255
- effective_provider = effective_call_args["provider"]
256
- effective_client = effective_call_args["client"]
269
+ # Get the appropriate provider call function with the possibly overridden provider
270
+ effective_provider = effective_call_args["provider"]
271
+ effective_client = effective_call_args["client"]
257
272
 
258
- if effective_provider in get_args(LocalProvider):
259
- provider_call, effective_client = _get_local_provider_call(
260
- cast(LocalProvider, effective_provider),
261
- effective_client,
262
- True,
263
- )
264
- effective_call_args["client"] = effective_client
265
- else:
266
- provider_call = _get_provider_call(
267
- cast(Provider, effective_provider)
268
- )
273
+ if effective_provider in get_args(LocalProvider):
274
+ provider_call, effective_client = _get_local_provider_call(
275
+ cast(LocalProvider, effective_provider),
276
+ effective_client,
277
+ True,
278
+ )
279
+ effective_call_args["client"] = effective_client
280
+ else:
281
+ provider_call = _get_provider_call(
282
+ cast(Provider, effective_provider)
283
+ )
269
284
 
270
- # Use the provider-specific call function with overridden args
271
- call_kwargs = dict(effective_call_args)
272
- del call_kwargs[
273
- "provider"
274
- ] # Remove provider as it's not a parameter to provider_call
285
+ # Use the provider-specific call function with overridden args
286
+ call_kwargs = dict(effective_call_args)
287
+ del call_kwargs["provider"] # Not a parameter to provider_call
275
288
 
276
- # Get decorated function using provider_call
277
- decorated = provider_call(**call_kwargs)(fn)
289
+ # Get decorated function using provider_call
290
+ decorated = provider_call(**call_kwargs)(fn)
278
291
 
279
- # Call the decorated function and wrap the result
280
- result = await decorated(*args, **kwargs)
281
- return _wrap_result(result)
292
+ # Call the decorated function and wrap the result
293
+ result = await decorated(*args, **kwargs)
294
+ return _wrap_result(result)
295
+
296
+ return context_bound_inner_async()
282
297
 
283
- inner_async._original_call_args = original_call_args # pyright: ignore [reportAttributeAccessIssue]
284
- inner_async._original_fn = fn # pyright: ignore [reportAttributeAccessIssue]
298
+ wrapper_with_context._original_call_args = original_call_args # pyright: ignore [reportAttributeAccessIssue]
299
+ wrapper_with_context._original_fn = fn # pyright: ignore [reportAttributeAccessIssue]
285
300
 
286
- return inner_async
301
+ return wrapper_with_context # pyright: ignore [reportReturnType]
287
302
  else:
288
303
 
289
304
  @wraps(fn)
mirascope/llm/_context.py CHANGED
@@ -114,10 +114,10 @@ def _context(
114
114
  client: Any | None = None, # noqa: ANN401
115
115
  call_params: CommonCallParams | Any | None = None, # noqa: ANN401
116
116
  ) -> LLMContext:
117
- """Context manager for synchronous LLM API calls.
117
+ """Context manager for LLM API calls.
118
118
 
119
119
  This is an internal method that allows both setting and structural overrides
120
- for synchronous functions.
120
+ for LLM functions.
121
121
 
122
122
  Unfortunately we have not yet identified a way to properly type hint this because
123
123
  providing no structural overrides means the return type is that of the original
@@ -138,7 +138,7 @@ def _context(
138
138
  client: The client to use for the LLM API call.
139
139
  call_params: The call parameters for the LLM API call.
140
140
 
141
- Yields:
141
+ Returns:
142
142
  The context object that can be used to apply the context to a function.
143
143
  """
144
144
  old_context: LLMContext | None = getattr(_current_context_local, "context", None)
@@ -171,16 +171,19 @@ def _context(
171
171
  )
172
172
 
173
173
 
174
- def apply_context_overrides_to_call_args(call_args: CallArgs) -> CallArgs:
174
+ def apply_context_overrides_to_call_args(
175
+ call_args: CallArgs, context_override: LLMContext | None = None
176
+ ) -> CallArgs:
175
177
  """Apply any active context overrides to the call arguments.
176
178
 
177
179
  Args:
178
180
  call_args: The original call arguments.
181
+ context_override: Optional explicit context to use instead of the current thread context.
179
182
 
180
183
  Returns:
181
184
  The call arguments with any context overrides applied.
182
185
  """
183
- context = get_current_context()
186
+ context = context_override or get_current_context()
184
187
  if not context:
185
188
  return call_args
186
189
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mirascope
3
- Version: 1.21.3
3
+ Version: 1.21.4
4
4
  Summary: LLM abstractions that aren't obstructions
5
5
  Project-URL: Homepage, https://mirascope.com
6
6
  Project-URL: Documentation, https://mirascope.com/WELCOME
@@ -332,8 +332,8 @@ mirascope/integrations/otel/_utils.py,sha256=SCVb3MpcpqLpCpumJEbEdINceNdusnyT6iu
332
332
  mirascope/integrations/otel/_with_hyperdx.py,sha256=f17uxXQk5zZPtyj6zwPwJz5i7atsnUPOoq1LqT8JO0E,1637
333
333
  mirascope/integrations/otel/_with_otel.py,sha256=tbjd6BEbcSfnsm5CWHBoHwbRNrHt6-t4or-SYGQSD-w,1659
334
334
  mirascope/llm/__init__.py,sha256=zo7QD39zQ6HIU_N2yHrzd6bbcT9YmlikZ84JN_r5k4E,397
335
- mirascope/llm/_call.py,sha256=XyMMZzg_DUW1pZgVcOP0QxyVDCYW8s7Xj_XMDwCzb_w,13068
336
- mirascope/llm/_context.py,sha256=Q4M-XRVsAvDW2LPW2XNtCm5HRU36BLFJYuahfGMfTGI,12332
335
+ mirascope/llm/_call.py,sha256=zPULSy5GLlJszphRvc3WUSFEsmP83Abp0FzbZefTa8Y,13848
336
+ mirascope/llm/_context.py,sha256=vtHJkLlFfUwyR_hYEHXAw3xunpHhLn67k4kuFw50GR8,12481
337
337
  mirascope/llm/_override.py,sha256=m4MdOhM-aJRIGP7NBJhscq3ISNct6FBPn3jjmryFo_Q,112292
338
338
  mirascope/llm/_protocols.py,sha256=HXspRAC0PduGqbh2BM0CGe5iVj7CC3ZKMPAYvFvbDNQ,16406
339
339
  mirascope/llm/_response_metaclass.py,sha256=6DLQb5IrqMldyEXHT_pAsr2DlUVc9CmZuZiBXG37WK8,851
@@ -368,7 +368,7 @@ mirascope/v0/base/ops_utils.py,sha256=1Qq-VIwgHBaYutiZsS2MUQ4OgPC3APyywI5bTiTAmA
368
368
  mirascope/v0/base/prompts.py,sha256=FM2Yz98cSnDceYogiwPrp4BALf3_F3d4fIOCGAkd-SE,1298
369
369
  mirascope/v0/base/types.py,sha256=ZfatJoX0Yl0e3jhv0D_MhiSVHLYUeJsdN3um3iE10zY,352
370
370
  mirascope/v0/base/utils.py,sha256=XREPENRQTu8gpMhHU8RC8qH_am3FfGUvY-dJ6x8i-mw,681
371
- mirascope-1.21.3.dist-info/METADATA,sha256=C4EyG1JBa6VMuS-WqgsCGClL3eLVtBN0dPzL1nJ6Rsg,8730
372
- mirascope-1.21.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
373
- mirascope-1.21.3.dist-info/licenses/LICENSE,sha256=LAs5Q8mdawTsVdONpDGukwsoc4KEUBmmonDEL39b23Y,1072
374
- mirascope-1.21.3.dist-info/RECORD,,
371
+ mirascope-1.21.4.dist-info/METADATA,sha256=_QcEbi9UfgJ4USOyRvAYgDaBBpsQIR_noP_C52Z1S_c,8730
372
+ mirascope-1.21.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
373
+ mirascope-1.21.4.dist-info/licenses/LICENSE,sha256=LAs5Q8mdawTsVdONpDGukwsoc4KEUBmmonDEL39b23Y,1072
374
+ mirascope-1.21.4.dist-info/RECORD,,