Skip to content

Commit

Permalink
Fix aconfig_with_context
Browse files Browse the repository at this point in the history
asyncio.Event is not thread-safe so it must be created in the asyncio thread
  • Loading branch information
cbornet committed Jan 10, 2025
1 parent 018666e commit 28266fa
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions libs/core/langchain_core/beta/runnables/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,11 @@ def _key_from_id(id_: str) -> str:

def _config_with_context(
config: RunnableConfig,
steps: list[Runnable],
context_specs: list[tuple[ConfigurableFieldSpec, int]],
setter: Callable,
getter: Callable,
event_cls: Union[type[threading.Event], type[asyncio.Event]],
) -> RunnableConfig:
if any(k.startswith(CONTEXT_CONFIG_PREFIX) for k in config.get("configurable", {})):
return config

context_specs = [
(spec, i)
for i, step in enumerate(steps)
for spec in step.config_specs
if spec.id.startswith(CONTEXT_CONFIG_PREFIX)
]
grouped_by_key = {
key: list(group)
for key, group in groupby(
Expand Down Expand Up @@ -134,8 +125,17 @@ async def aconfig_with_context(
Returns:
The patched runnable config.
"""
return await asyncio.to_thread(
_config_with_context, config, steps, _asetter, _agetter, asyncio.Event
if any(k.startswith(CONTEXT_CONFIG_PREFIX) for k in config.get("configurable", {})):
return config

context_specs = [
(spec, i)
for i, step in enumerate(steps)
for spec in await asyncio.to_thread(getattr, step, "config_specs")
if spec.id.startswith(CONTEXT_CONFIG_PREFIX)
]
return _config_with_context(
config, context_specs, _asetter, _agetter, asyncio.Event
)


Expand All @@ -152,7 +152,18 @@ def config_with_context(
Returns:
The patched runnable config.
"""
return _config_with_context(config, steps, _setter, _getter, threading.Event)
if any(k.startswith(CONTEXT_CONFIG_PREFIX) for k in config.get("configurable", {})):
return config

context_specs = [
(spec, i)
for i, step in enumerate(steps)
for spec in step.config_specs
if spec.id.startswith(CONTEXT_CONFIG_PREFIX)
]
return _config_with_context(
config, context_specs, _setter, _getter, threading.Event
)


@beta()
Expand Down

0 comments on commit 28266fa

Please sign in to comment.