|
20 | 20 | TypeVar, |
21 | 21 | ) |
22 | 22 |
|
23 | | -from typing_extensions import override |
| 23 | +from typing_extensions import override, reveal_type |
24 | 24 |
|
25 | 25 | # Re-export create-model for backwards compatibility |
26 | 26 | from langchain_core.utils.pydantic import create_model # noqa: F401 |
@@ -132,35 +132,23 @@ def asyncio_accepts_context() -> bool: |
132 | 132 | _T = TypeVar("_T") |
133 | 133 |
|
134 | 134 |
|
135 | | -def task_with_context( |
136 | | - coro: Coroutine[Any, Any, _T], context: Context |
137 | | -) -> asyncio.Task[_T]: |
138 | | - """Create a task with a context. |
139 | | -
|
140 | | - Args: |
141 | | - coro: The coroutine to create a task for. |
142 | | - context: The context to use. |
143 | | -
|
144 | | - Returns: |
145 | | - The task with the context. |
146 | | - """ |
147 | | - if asyncio_accepts_context(): |
148 | | - return asyncio.create_task(coro, context=context) # type: ignore[arg-type,call-arg,unused-ignore] |
149 | | - return asyncio.create_task(coro) |
150 | | - |
151 | | - |
152 | | -def coro_with_context(coro: Awaitable[_T], context: Context) -> Awaitable[_T]: |
| 135 | +def coro_with_context( |
| 136 | + coro: Awaitable[_T], context: Context, *, create_task: bool = False |
| 137 | +) -> Awaitable[_T]: |
153 | 138 | """Await a coroutine with a context. |
154 | 139 |
|
155 | 140 | Args: |
156 | 141 | coro: The coroutine to await. |
157 | 142 | context: The context to use. |
| 143 | + create_task: Whether to create a task. |
158 | 144 |
|
159 | 145 | Returns: |
160 | 146 | The coroutine with the context. |
161 | 147 | """ |
162 | 148 | if asyncio_accepts_context(): |
163 | 149 | return asyncio.create_task(coro, context=context) # type: ignore[arg-type,call-arg,unused-ignore] |
| 150 | + if create_task: |
| 151 | + return asyncio.create_task(coro) # type: ignore[arg-type] |
164 | 152 | return coro |
165 | 153 |
|
166 | 154 |
|
|
0 commit comments