fix: avoid nil session dereference in OpenAI local mode#2233
Open
anxkhn wants to merge 1 commit into
Open
Conversation
The OpenAI agent executor built by KAgentApp.build_local() is constructed with session_factory=None, so execute() streams events with session=None. _stream_agent_events then built SessionContext(session_id=session.session_id) unconditionally, raising AttributeError before the agent ran and failing every local-mode A2A request with "'NoneType' object has no attribute 'session_id'". Fall back to the A2A context id when there is no session, matching the session identifier already used elsewhere in this executor. Add regression tests that exercise execute() with session_factory=None. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the OpenAI A2A agent executor when running in local mode (where session_factory=None) by avoiding dereferencing session.session_id when session is None, and adds regression tests to ensure local-mode execution reaches the runner and uses a stable session identifier.
Changes:
- Update
_stream_agent_events()to derive aSessionContext.session_ideven whensessionisNone. - Add async tests covering
execute()behavior whensession_factory=None, including validating theSessionContextid fallback.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/kagent-openai/src/kagent/openai/_agent_executor.py | Avoids None session dereference by computing a fallback session id for SessionContext. |
| python/packages/kagent-openai/tests/test_agent_executor.py | Adds regression tests for local-mode execution and SessionContext session id fallback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+113
to
+116
| # In local mode there is no session_factory, so session is None; fall back to | ||
| # the A2A context id, which is the session identifier used elsewhere here. | ||
| session_id = session.session_id if session else context.context_id | ||
| session_context = SessionContext(session_id=session_id) |
Collaborator
There was a problem hiding this comment.
I agree with this comment - check for the session_id on context first, and then fallback to context_id, so it's same as in execute
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.
That dereference raises
AttributeError: 'NoneType' object has no attribute 'session_id'before the agent runs, so no local-mode request ever reaches themodel.
Fix
Fall back to the A2A context id when there is no session:
context.context_idis already howexecute()derives the session id(
getattr(context, "session_id", None) or context.context_id) and is theidentifier used throughout this executor, so the
SessionContextstaysconsistent and stable per A2A conversation.
Testing
Added
python/packages/kagent-openai/tests/test_agent_executor.py, which buildsthe executor with
session_factory=Noneand drivesexecute():Runner.run_streamed(before the fix itfails with the
AttributeErrorabove),SessionContextid falls back tocontext.context_id.Both fail on
mainand pass with the fix.