Skip to content

fix(litellm): injection_mode, context manager restore, validation, error consistency#1711

Draft
DK09876 wants to merge 4 commits into
mainfrom
dk/litellm-api-fixes
Draft

fix(litellm): injection_mode, context manager restore, validation, error consistency#1711
DK09876 wants to merge 4 commits into
mainfrom
dk/litellm-api-fixes

Conversation

@DK09876
Copy link
Copy Markdown
Contributor

@DK09876 DK09876 commented May 22, 2026

Summary

  • configure() footgun removedconfigure() without bank_id no longer silently uses "default". is_configured() and enable() now both require an explicit bank_id.
  • injection_mode actually honouredPREPEND_USER now prepends memories to the last user message; previously all monkeypatch paths ignored the setting and always injected into a system message.
  • hindsight_memory() full restore — context manager finally block now atomically restores all settings via _restore_config(). Previously 6 fields were silently lost on exit: sync_storage, tags, recall_tags, recall_tags_match, reflect_context, reflect_response_schema.
  • defaults.query wired up — was dead code; _inject_memories() now uses it as a fallback query when no hindsight_query kwarg is passed.
  • HindsightError consistency_inject_memories() and both log_pre_api_call methods in callbacks.py now raise HindsightError instead of ValueError for missing bank_id.
  • Input validationconfigure() and set_defaults() validate budget and recall_tags_match, raising ValueError immediately on bad values. document_id emits DeprecationWarning.
  • Thread safety_enabled_lock and _debug_lock protect shared mutable state in enable()/disable() and _last_injection_debug.

Test plan

  • uv run pytest tests/test_integration.py -v — 83 unit tests, all pass (added TestValidation, TestInjectionMode, TestQueryField, TestHindsightErrorConsistency, TestContextManagerFullRestore)
  • E2E suite against live Hindsight — 23 tests pass including new TestDesignFixes class that verifies each fix end-to-end

🤖 Generated with Claude Code

DK09876 and others added 4 commits May 21, 2026 15:22
…fault URL

- recall(): add include_entities, trace, recall_tags, recall_tags_match params
  (previously only supported via the callback/enable() path, not the manual API)
- reflect(): add recall_tags, recall_tags_match params (same gap)
- hindsight_memory(): default URL now matches configure()/wrap_openai()/wrap_anthropic()
  instead of hardcoding localhost; add session_id, use_reflect, reflect_context,
  tags, recall_tags, recall_tags_match params
- Document that enable() and HindsightCallback are mutually exclusive injection
  paths to prevent accidental double injection
- Add 17 tests covering all new behaviour

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… and add sync param to aretain

- hindsight_bank_id kwarg was leaking into LiteLLM as extra_body, causing
  OpenAI 400 errors; now popped in completion(), _wrapped_completion(),
  _wrapped_acompletion() and propagated as bank_id_override throughout
  injection and storage paths
- _inject_memories() accepts bank_id_override to honour per-call bank
  without mutating globals
- _store_conversation() and _store_conversation_from_text() accept
  bank_id_override for consistent per-call storage routing
- _LiteLLMStreamWrapper and _LiteLLMAsyncStreamWrapper carry
  bank_id_override so streamed responses store to the right bank
- aretain() now accepts sync=True, forwarding it to retain()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…estore, validation, error consistency

- config.py: remove DEFAULT_BANK_ID footgun (configure() without bank_id now
  leaves bank_id=None; is_configured() and enable() correctly require explicit
  bank_id). Add _restore_config() for atomic state restoration. Add
  budget/recall_tags_match validation in configure() and set_defaults().
  Emit DeprecationWarning for document_id usage.

- __init__.py: _inject_memories() now respects injection_mode
  (PREPEND_USER prepends to last user message; SYSTEM_MESSAGE keeps existing
  behaviour). Wire up defaults.query as fallback recall query. Fix
  ValueError → HindsightError for missing bank_id. hindsight_memory()
  finally block now calls _restore_config() to atomically restore all settings
  (previously lost: sync_storage, tags, recall_tags, recall_tags_match,
  reflect_context, reflect_response_schema). Add _enabled_lock and _debug_lock
  for thread safety on shared mutable state.

- callbacks.py: ValueError → HindsightError in log_pre_api_call and
  async_log_pre_api_call for missing bank_id, consistent with __init__.py.

- tests: update tests that relied on DEFAULT_BANK_ID behaviour; add
  TestValidation, TestInjectionMode, TestQueryField, TestHindsightErrorConsistency,
  TestContextManagerFullRestore (83 tests, all passing).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…t-bank-id behaviour

- Run ruff format on __init__.py and wrappers.py to match CI lint expectations
- test_config.py: update test_configure_with_no_arguments to assert bank_id is None
  (not DEFAULT_BANK_ID) and rename test_is_configured_true_with_defaults to
  test_is_configured_false_without_explicit_bank_id with corrected assertion,
  matching the removed DEFAULT_BANK_ID footgun

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@DK09876 DK09876 marked this pull request as draft May 22, 2026 17:03
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