Compare commits

...
Author SHA1 Message Date
Andrew MaguireandGitHub 57546d29e6 fix(llma): LangChain 1.0+ compatibility for CallbackHandler (#363)
* fix: Add LangChain 1.0+ compatibility for CallbackHandler imports

- Use try/except to import from langchain_core first (LangChain 1.0+)
- Fall back to legacy langchain imports for older versions
- Maintains backward compatibility with LangChain 0.x
- All existing tests pass (45 passed)

Fixes #362

* test: Add regression test for AgentAction/AgentFinish imports

- Tests that AgentAction and AgentFinish can be imported
- Tests on_agent_action and on_agent_finish callbacks with mock data
- Ensures compatibility with both LangChain 0.x and 1.0+
- Catches the import issue that was previously only tested with API keys

This addresses a test coverage gap identified during code review.

* chore: Add CHANGELOG entry for LangChain 1.0+ compatibility fix

* fix: Remove unused type: ignore comments for mypy

The type: ignore comments were only needed when the except block
executes, but CI runs with LangChain 1.0+ so the try block succeeds.
Mypy flags these as unused-ignore errors.

* chore: bump version to 6.7.12 for langchain 1.0 compatibility
2025-11-02 17:09:33 +00:00
4 changed files with 54 additions and 4 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
# Unreleased
# 6.7.12 - 2025-11-02
- fix(django): Restore process_exception method to capture view and downstream middleware exceptions (fixes #329)
- fix(ai/langchain): Add LangChain 1.0+ compatibility for CallbackHandler imports (fixes #362)
# 6.7.11 - 2025-10-28
+8 -2
View File
@@ -20,8 +20,14 @@ from typing import (
)
from uuid import UUID
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinish
try:
# LangChain 1.0+ and modern 0.x with langchain-core
from langchain_core.callbacks.base import BaseCallbackHandler
from langchain_core.agents import AgentAction, AgentFinish
except (ImportError, ModuleNotFoundError):
# Fallback for older LangChain versions
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinish
from langchain_core.documents import Document
from langchain_core.messages import (
AIMessage,
@@ -1877,3 +1877,46 @@ def test_tool_definition(mock_client):
assert props["$ai_latency"] == 1.0
# Verify that tools are captured in the $ai_tools property
assert props["$ai_tools"] == tools
def test_agent_action_and_finish_imports():
"""
Regression test for LangChain 1.0+ compatibility (Issue #362).
Verifies that AgentAction and AgentFinish can be imported and used.
This test ensures the imports work with both LangChain 0.x and 1.0+.
"""
# Import the types that caused the compatibility issue
try:
from langchain_core.agents import AgentAction, AgentFinish
except (ImportError, ModuleNotFoundError):
from langchain.schema.agent import AgentAction, AgentFinish # type: ignore
# Verify they're available in the callbacks module
from posthog.ai.langchain.callbacks import CallbackHandler
# Test on_agent_action with mock data
mock_client = MagicMock()
callbacks = CallbackHandler(mock_client)
run_id = uuid.uuid4()
parent_run_id = uuid.uuid4()
# Create mock AgentAction
action = AgentAction(tool="test_tool", tool_input="test_input", log="test_log")
# Should not raise an exception
callbacks.on_agent_action(action, run_id=run_id, parent_run_id=parent_run_id)
# Verify parent was set
assert run_id in callbacks._parent_tree
assert callbacks._parent_tree[run_id] == parent_run_id
# Test on_agent_finish with mock data
finish = AgentFinish(return_values={"output": "test_output"}, log="finish_log")
# Should not raise an exception
callbacks.on_agent_finish(finish, run_id=run_id, parent_run_id=parent_run_id)
# Verify capture was called
assert mock_client.capture.call_count == 1
call_args = mock_client.capture.call_args[1]
assert call_args["event"] == "$ai_span"
+1 -1
View File
@@ -1,4 +1,4 @@
VERSION = "6.7.11"
VERSION = "6.7.12"
if __name__ == "__main__":
print(VERSION, end="") # noqa: T201