fix(pydantic): use current_context() in RestateModelWrapper.request_stream (AttributeError on streaming / event_stream_handler)#201
Open
selimacerbas wants to merge 1 commit into
Conversation
…tream request_stream() referenced `self._context`, which is never assigned anywhere in restate.ext.pydantic. The sibling request() and every other method use the current_context() extension point (added in restatedev#148) -- request_stream was simply missed in that migration. Any agent run that uses `event_stream_handler` (the only way restate.ext.pydantic surfaces per-turn model events) therefore raises AttributeError as soon as the streaming model path executes: File ".../restate/ext/pydantic/_model.py", line 116, in request_stream response = await self._context.run_typed("Model stream call", ...) File ".../pydantic_ai/models/wrapper.py", in __getattr__ return getattr(self.wrapped, item) AttributeError: 'OpenAIChatModel' object has no attribute '_context' (self._context -> WrapperModel.__getattr__ -> delegates to the wrapped model -> no such attribute.) Mirror request(): fetch current_context(), raise UserError when it is None, then use it for run_typed("Model stream call", ...). current_context and UserError are already imported in this module.
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
Author
|
I have read the CLA Document and I hereby sign the CLA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RestateModelWrapper.request_streamreferencesself._context, which is never assigned anywhere inrestate.ext.pydantic. The siblingrequest()method — and every other method in_model.py/_toolset.py/_agent.py— uses thecurrent_context()extension point added in #148.request_streamwas missed in that migration, so any agent run that usesevent_stream_handler(the only wayrestate.ext.pydanticsurfaces per-turn model events) fails the moment the streaming model path executes.self._context→WrapperModel.__getattr__→ delegates to the wrapped model →AttributeError.Reproduction
Build a
RestateAgent(agent, event_stream_handler=handler)andagent.run(...)it inside a Restate handler:(
event_stream_handleris required for streaming because pydantic-ai routes through the streaming model path —_agent_graph.py_streaming_handler→req_ctx.model.request_stream(...)— only when a handler is set.)Fix
Use
current_context()inrequest_stream, mirroringrequest()(including itsNoneguard). Bothcurrent_contextandUserErrorare already imported in this module.Test
Not included here — happy to add one in your preferred harness. A targeted test would run a
RestateAgentwith anevent_stream_handler(e.g. aTestModel/FunctionModel) under a Restate context and assert the handler receives the stream and the run completes; it raisesAttributeErrorbefore this change. Point me at the right module and I'll add it.Notes
Noneguard for parity withrequest()); no public API change.restate-sdk[pydantic]==0.18.1; the bug is also present onmain.