Skip to content

LangChain 1.0+ compatibility: Update imports to use langchain_core #362

@andrewm4894

Description

@andrewm4894

Bug Description

The PostHog LangChain CallbackHandler is incompatible with LangChain 1.0+ due to deprecated import paths.

Current Behavior

When attempting to use the CallbackHandler with LangChain 1.0+:

from posthog.ai.langchain import CallbackHandler as PostHogCallbackHandler

This raises:

ModuleNotFoundError: No module named 'langchain.callbacks'

Root Cause

The issue is in posthog/ai/langchain/callbacks.py lines 23-24:

from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinish

LangChain 1.0 (released September 2024) restructured the codebase and moved these modules to langchain_core.

Environment

  • PostHog SDK version: 6.7.11
  • LangChain version: 1.0.3
  • Python: 3.x

Suggested Fix

Update the imports in posthog/ai/langchain/callbacks.py lines 23-24:

# Current (incompatible with LangChain 1.0+)
from langchain.callbacks.base import BaseCallbackHandler
from langchain.schema.agent import AgentAction, AgentFinish

# Proposed fix
from langchain_core.callbacks.base import BaseCallbackHandler
from langchain_core.agents import AgentAction, AgentFinish

This change maintains backward compatibility since langchain_core exists in both LangChain 0.x and 1.0+.

Additional Context

LangChain 1.0 removed the langchain.callbacks and langchain.schema modules as part of the core refactoring. All core functionality was moved to the langchain_core package, which is the proper import path for both old and new versions.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions