pydantic ext: request_stream() never initializes the turnstile — KeyError('call_...') on first tool call of a streamed run
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 76/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Tranquilo
- Stack tecnológico
- python
- Área
- distributed-systems
Línea de trabajo
Comienza en restate/ext/pydantic/_model.py comparando RestateModelWrapper.request con request_stream; después inspecciona _toolset.py y turnstile.py para seguir la búsqueda que falla. Verifica que las respuestas transmitidas inicialicen el turnstile a partir de sus tool-call IDs y que una ejecución transmitida con varias llamadas a herramientas finalice sin el KeyError.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Summary
In a streamed run (RestateAgent with an event_stream_handler), the first tool call fails with:
KeyError: 'call_XXXXXXXXXXXX'
raised from Turnstile.wait_for via RestateContextRunToolSet.call_tool.
Non-streamed runs (no event_stream_handler) work fine.
Environment
- restate-sdk 1.0.3 (bug also present on current
main) - pydantic-ai-slim 2.4.0
- Python 3.12, Restate server 1.7.2
Root cause
Tool executions are gated by a Turnstile built from the model response's tool-call ids so parallel tool calls run in deterministic journal order.
The non-streaming path arms it — restate/ext/pydantic/_model.py, RestateModelWrapper.request:
res = await context.run_typed("Model call", self.wrapped.request, self._options, *args, **kwargs)
ids = [c.tool_call_id for c in res.tool_calls]
current_state().turnstile = Turnstile(ids) # <- line 82
The streaming path (RestateModelWrapper.request_stream) never does this, so the state keeps the default Turnstile([]) from State.__init__. Then in _toolset.py::RestateContextRunToolSet.call_tool:
await turnstile.wait_for(id) # Turnstile.wait_for: self.events[id] -> KeyError
Reproduction
Same setup as the companion issue (agent with one tool, RestateAgent(agent, event_stream_handler=..., auto_wrap_tools=True)), with the "run event" coroutine bug patched or avoided. Any prompt that triggers a tool call raises:
File ".../restate/ext/pydantic/_toolset.py", line 92, in call_tool
await turnstile.wait_for(id)
File ".../restate/ext/turnstile.py", line 25, in wait_for
event = self.events[id]
KeyError: 'call_HtCkhcyMnGofOXHadHWA9AXS'
Suggested fix
Mirror the non-streaming path after the journaled stream step resolves (deterministic on replay since the response is recorded):
response = await context.run_typed("Model stream call", request_stream_run, self._options)
ids = [c.tool_call_id for c in response.tool_calls]
current_state().turnstile = Turnstile(ids)
yield RestateStreamedResponse(model_request_parameters, response)
We are running this exact patch as a local subclass workaround and streamed multi-tool runs (3 tool calls in one turn) complete correctly with proper Calling <tool> journal entries.
Related
- Companion issue: #222 (the two bugs stack on the same code path).
- #198 fixed a similar turnstile race in the langchain integration.
- Lenguaje dominante
- Python
- Estrellas
- 82
- Forks
- 24
- Merge medio
- 13 min
- PR fusionados (30 d)
- 1
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de restatedev/sdk-python
-
Dificultad 2/5 1-2 días Aptitud para principiantes 68/100
restatedev/sdk-python#204 · 1 comentario ·
-
RestateModelWrapper should not wrap `SdkInternalBaseException` as generic Exception; classify non-retryable provider errors as terminalPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
restatedev/sdk-python#225 ·
-
pydantic ext: auto_wrap_tools + async event_stream_handler crashes with 'Object of type coroutine is not JSON serializable' on first tool callPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
restatedev/sdk-python#222 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
restatedev/sdk-python#221 ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 38/100
restatedev/sdk-python#205 · 1 comentario ·
Todos los issues de restatedev/sdk-python
Issues similares
-
bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
debpalash/VoiceStudio#2624 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
SQLGlot translates Spark concat_ws on an array into array stringificationPosiblemente ocupada @ansh-rohilla la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
Los mantenedores suelen responder en 1 día
-
Make Catch2 optional when `RDK_BUILD_CPP_TESTS=OFF`Posiblemente ocupada @pechersky la tomó hoy. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 2 días