Skip to content

fix(core): improve typing/docs for on_chat_model_start to clarify required positional args#35324

Merged
ccurme (ccurme) merged 3 commits intolangchain-ai:masterfrom
gitbalaji:fix/core-handle-event-index-error
Feb 22, 2026
Merged

fix(core): improve typing/docs for on_chat_model_start to clarify required positional args#35324
ccurme (ccurme) merged 3 commits intolangchain-ai:masterfrom
gitbalaji:fix/core-handle-event-index-error

Conversation

@gitbalaji
Copy link
Copy Markdown
Contributor

Summary

Rework of #35323 based on maintainer feedback from ccurme (@ccurme).

"The on_chat_model_start has 2 required arguments, this should just be typed / documented better."

Instead of a runtime len(args) >= 2 guard, this PR improves the documentation on on_chat_model_start to make the required signature contract explicit.

What changed

  • Reverted the runtime guard (_MIN_ARGS_FOR_CHAT_MODEL_FALLBACK constant and len(args) checks) from handle_event() and _ahandle_event_for_handler() in manager.py
  • Improved docstrings on on_chat_model_start in both CallbackManagerMixin (sync) and AsyncCallbackHandler (async) in base.py, with an explicit cd /Users/balajiseshadri/workspace/langchain && git push origin fix/core-handle-event-index-error --force-with-lease! note block:
    • States that serialized and messages are required positional arguments
    • Warns that using *args in an override suppresses these names, causing an IndexError in the fallback path to on_llm_start
    • Provides a correct example signature
  • Updated tests in test_handle_event.py — removed invalid-usage tests (no-args, one-arg) that only tested the reverted guard; kept tests for the valid fallback path (correct signature + NotImplementedError)

Why

The 2 required positional args (serialized, messages) are always provided by the internal call sites in manager.py. The IndexError only occurs when user-written handlers wrongly use *args in their override, violating the signature contract. The right fix is to make this contract clear in the documentation.

Closes #31576

…t fallback lacks args

When a callback handler raises NotImplementedError for on_chat_model_start,
the fallback to on_llm_start accesses args[1] without checking if sufficient
args were provided, causing an IndexError that masks the original error.

Add a len(args) guard before the fallback in both sync handle_event() and
async _ahandle_event_for_handler(). When args are insufficient, log a
warning instead of crashing.

Closes langchain-ai#31576

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ired positional args

Per maintainer feedback from @ccurme on issue langchain-ai#31576:
'The on_chat_model_start has 2 required arguments, this should just be
typed / documented better.'

Changes:
- Revert runtime len(args) guard from handle_event() and
  _ahandle_event_for_handler() — unnecessary because the callback
  manager always calls on_chat_model_start with both required positional
  args (serialized, messages)
- Add a clear note docblock to both sync and async on_chat_model_start
  in base.py, warning implementors to declare serialized and messages as
  named positional params (not *args) to avoid IndexError in the
  on_llm_start fallback path
- Update test_handle_event.py to cover only valid usage (correct
  signature + NotImplementedError fallback); remove invalid-usage tests
  that only exercised the reverted guard

Closes langchain-ai#31576
@github-actions github-actions Bot added core `langchain-core` package issues & PRs external fix For PRs that implement a fix labels Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

handle_event raises IndexError when handler raises NotImplementedError

3 participants