feat(sdk): LlamaIndex callback adapter - #125
Merged
Merged
Conversation
Add a LlamaIndex BaseCallbackHandler so a LlamaIndex query or agent is governed by a RiskKernel run with ~no code change — the LlamaIndex analog of the LangChain adapter. RiskKernelCallbackHandler ticks one governed step per LLM call (CBEventType.LLM on_event_start), so a run's loop/time budget is enforced over the outer LlamaIndex loop and a halt surfaces as BudgetExceeded. LlamaIndex's CallbackManager doesn't wrap handler calls in a try/except, so the halt propagates to the caller without any extra flag (unlike LangChain's raise_error). With gate_tools=True, FUNCTION_CALL events route through the approval gate. llama-index-core is imported lazily with an object fallback, so the module imports and the SDK installs without llama-index present (it is not a dependency). Pinned to the llama-index-core >= 0.10 callback protocol. Tests exercise the governance path on stdlib alone against a fake Run (step tick, loop-budget halt, tool gating allow/deny, event-type normalization), plus a skip-unless-installed integration test that proves a runaway loop of real LlamaIndex LLM calls actually stops. Full suite passes; README and CHANGELOG updated.
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.
Adds a LlamaIndex integration to the Python SDK so a LlamaIndex query or agent is governed by a RiskKernel run with ~no code change — the LlamaIndex analog of the existing LangChain adapter.
What it does
RiskKernelCallbackHandler(inriskkernel/adapters/llama_index.py) is a LlamaIndexBaseCallbackHandler:CBEventType.LLMon_event_start), so a run's loop/time budget is enforced over the outer LlamaIndex loop. The next step over budget raisesBudgetExceeded, halting the query/agent.CallbackManager.on_event_startdoesn't wrap handler calls in a try/except, so the exception propagates to the caller — no extra flag needed (unlike LangChain'sraise_error). I verified this in the LlamaIndex source and with a real integration test.gate_tools=True) routesCBEventType.FUNCTION_CALLevents through the approval gate, mirroring the LangChain adapter'sgate_toolsoption.llama-index-coreis imported lazily with anobjectfallback base class, so the module imports and the SDK installs without llama-index present — it is not a dependency of the SDK. Pinned to thellama-index-core>= 0.10 callback protocol (documented in the module docstring).Register it on
Settings.callback_managerand governance is invisible until the budget bites:Callback events hooked
CBEventType.LLM("llm") —on_event_startticksrun.step()(loop/time enforcement).CBEventType.FUNCTION_CALL("function_call") —on_event_startgated through the approval gate whengate_tools=True.on_event_end/start_trace/end_trace— implemented as no-ops (the protocol requires them);on_event_startreturns theevent_idso the callback manager can correlate the matching end event.Tests
tests/test_llama_index.pyexercises the governance path on stdlib alone against a fakeRun: per-LLM-call step tick, loop-budget halt propagation, non-LLM events not ticking a step, tool-gating allow/deny, and event-type (CBEventType-like) normalization. A skip-unless-installed integration test proves a runaway loop of real LlamaIndex LLM calls actually stops at the budget. I ran the integration test againstllama-index-core0.14.22 in a throwaway venv and it passes; the full suite passes stdlib-only (25 tests, 3 framework-gated skips):README and CHANGELOG updated.
Closes #86.