fix: avoid nil session dereference in OpenAI local mode#2233
Open
anxkhn wants to merge 2 commits into
Open
Conversation
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.
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>
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
anxkhn
force-pushed
the
fix/openai-local-nil-session
branch
from
July 16, 2026 06:44
2c0272d to
b06d545
Compare
Contributor
Author
|
Pushed a revision addressing the latest review/CI feedback. |
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.
What
The OpenAI agent executor crashes on every request when the A2A server is run
in local mode.
KAgentApp.build_local()constructs the executor withsession_factory=None(
python/packages/kagent-openai/src/kagent/openai/_a2a.py), soexecute()leaves
sessionasNone._stream_agent_eventsthen built theSessionContextwithsession.session_idunconditionally:That dereference raises
AttributeError: 'NoneType' object has no attribute 'session_id'before the agent runs, so no local-mode request ever reaches themodel.
Fix
Use the persisted session id when a session exists. In local mode, check
context.session_idfirst and then fall back tocontext.context_id:This matches the identifier derivation in
execute(), keeping metadata and theSessionContextpassed toRunner.run_streamedconsistent.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,context.session_idtakes precedence when provided.