Skip to content

feat(litellm,llamaindex): LiteLLM dual instrumentation + LlamaIndex streaming safety - #6

Merged
manas-fortifyroot merged 1 commit into
fr-v0.52.6.xfrom
feature/phase-1h-litellm-dual-instrumentation
Mar 24, 2026
Merged

feat(litellm,llamaindex): LiteLLM dual instrumentation + LlamaIndex streaming safety#6
manas-fortifyroot merged 1 commit into
fr-v0.52.6.xfrom
feature/phase-1h-litellm-dual-instrumentation

Conversation

@manas-fortifyroot

Copy link
Copy Markdown

Summary

LiteLLM — dual instrumentation:

  • Add _FortifyRootCompletionLogger (duck-typed CustomLogger) registered at litellm.callbacks[0] so it masks response_obj in-place before LiteLLM's native OTel callback fires — native OTel sees already-masked content, and litellm_request becomes a child of FR's span automatically via ambient OTel context.
  • FR safety span (fortifyroot.litellm.safety, fortifyroot.span.role=safety_wrapper) set as ambient context via set_span_in_context + SUPPRESS_LANGUAGE_MODEL_INSTRUMENTATION_KEY; token passed through to streaming wrappers and detached in finally only after stream exhaustion.
  • Bug fix: _finalize_awaitable_response now sets FR span as ambient context (was suppress-only; callbacks on awaitable path emitted findings on wrong span).
  • New test_logger_integration.py (10 FR tests): logger skip on stream, non-stream masking, finding emission on correct span, callback registration, non-list callbacks, span role attribute.
  • test_safety_hooks.py: updated for new span name/event counts/role attribute; new test_sync_wrapper_awaitable_response_fr_span_is_ambient.

LlamaIndex — streaming safety:

  • New streaming_safety.py: holdback-buffer streaming masking for LlamaIndex async streams.
  • Update dispatcher_wrapper.py and safety.py to wire streaming safety through the instrumentation layer.
  • New test_streaming_safety.py with FR-marked tests.

Test plan

  • ./scripts/run-tests.sh --fr --package '*litellm*' — 39 passed, 0 failed
  • ./scripts/run-tests.sh --fr --package '*llamaindex*' — passes including new streaming safety tests
  • ./scripts/run-tests.sh --fr — 338 passed, 0 failed across all packages

…treaming safety

LiteLLM — dual instrumentation design:
- Add _FortifyRootCompletionLogger (duck-typed CustomLogger) registered at
  litellm.callbacks[0] so it fires before LiteLLM's native OTel callback.
  Masks response_obj in-place for non-streaming calls; streaming handled
  per-chunk by streaming_safety.py.
- FR safety span (fortifyroot.litellm.safety, role=safety_wrapper) set as
  ambient OTel context via set_span_in_context so LiteLLM's litellm_request
  span is automatically created as a child — no native OTel config change needed.
- streaming_safety.py: accept token= param; detach in finally so FR span stays
  ambient for the full stream including end-of-stream callbacks.
- Bug fix: _finalize_awaitable_response now sets FR span as ambient AND suppress
  key (was suppress-only; callbacks on awaitable path fired on wrong span).
- New test_logger_integration.py: 10 FR-marked tests covering logger skip on
  stream, non-stream masking, finding emission on current span, callback
  registration at position 0, non-list callbacks, span role attribute.
- test_safety_hooks.py: updated for new span name, event counts, role attribute;
  new test_sync_wrapper_awaitable_response_fr_span_is_ambient.

LlamaIndex — streaming safety:
- Add streaming_safety.py with holdback-buffer streaming masking for LlamaIndex
  dispatcher_wrapper async streams.
- Update dispatcher_wrapper.py and safety.py to wire streaming safety through
  the LlamaIndex instrumentation layer.
- New test_streaming_safety.py with FR-marked tests for streaming safety behavior.
@manas-fortifyroot
manas-fortifyroot merged commit c26376b into fr-v0.52.6.x Mar 24, 2026
3 checks passed
@manas-fortifyroot
manas-fortifyroot deleted the feature/phase-1h-litellm-dual-instrumentation branch March 24, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant