Skip to content

Conversation

@akshaydeo
Copy link
Contributor

Summary

  • enable instrumentation-based Langchain integration without callbacks
  • export the new helper and document feature in changelog
  • implement traces, spans and generations for Langchain methods
  • capture tool runs as tool calls
  • instrument retrievals and other LLM methods

Testing

  • python3 -m pytest -q (fails: ModuleNotFoundError: No module named 'filetype')

https://chatgpt.com/codex/tasks/task_e_6849518ba724832088860946f02f7372

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 11, 2025

Summary by CodeRabbit

  • New Features
    • Introduced instrumentation-based integration for LangChain, enabling automatic tracing and logging of LLM calls, chains, agents, tools, and retrievers.
    • Extended support for tracing to include retrievals and streaming methods.
  • Documentation
    • Updated the changelog in the documentation to include details for versions 3.8.4 and 3.8.5, highlighting the new LangChain integration features.

Walkthrough

The changes introduce a new module for LangChain instrumentation, enabling automatic tracing and logging of LLM calls, chains, agents, tools, and retrievers within the Maxim SDK. Public exports are updated to include the instrumentation function, and the README changelog is updated to reflect these additions in versions 3.8.4 and 3.8.5.

Changes

File(s) Change Summary
README.md Updated changelog with entries for versions 3.8.4 and 3.8.5, describing LangChain instrumentation features.
maxim/logger/langchain/init.py Added import and public export of instrument_langchain from the new instrumenter module.
maxim/logger/langchain/instrumenter.py New module implementing monkey-patching for LangChain classes to enable automatic tracing and logging.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant LangChainClass
    participant MaximInstrumenter
    participant MaximSDK

    User->>LangChainClass: Call method (e.g., invoke)
    activate LangChainClass
    LangChainClass->>MaximInstrumenter: Wrapped method intercepts call
    MaximInstrumenter->>MaximSDK: Start trace/span/generation
    MaximInstrumenter->>LangChainClass: Call original method
    LangChainClass-->>MaximInstrumenter: Return result or error
    MaximInstrumenter->>MaximSDK: Log result or error, end trace/span
    MaximInstrumenter-->>User: Return result or raise error
Loading

Poem

In the warren of code, new traces appear,
LangChain now whispers, “Maxim is here!”
With every agent and tool, the logs brightly gleam,
Streaming and retrieval, all part of the dream.
A hop and a patch, let the rabbit rejoice—
Now every LLM call has a Maxim voice!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch codex/add-langchain-instrumentation-integration
  • Post Copyable Unit Tests in Comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f755339 and a1079f9.

📒 Files selected for processing (3)
  • README.md (1 hunks)
  • maxim/logger/langchain/__init__.py (2 hunks)
  • maxim/logger/langchain/instrumenter.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
maxim/logger/langchain/__init__.py (1)
maxim/logger/langchain/instrumenter.py (1)
  • instrument_langchain (41-41)
🪛 Pylint (3.3.7)
maxim/logger/langchain/__init__.py

[error] 2-2: Cannot import 'instrumenter' due to 'expected an indented block after function definition on line 41 (maxim.logger.langchain.instrumenter, line 42)'

(E0001)

maxim/logger/langchain/instrumenter.py

[error] 42-42: Parsing failed: 'expected an indented block after function definition on line 41 (maxim.logger.langchain.instrumenter, line 42)'

(E0001)

🪛 Ruff (0.11.9)
maxim/logger/langchain/instrumenter.py

42-42: SyntaxError: Expected an indented block after function definition


50-50: SyntaxError: Unexpected indentation


70-70: Trailing comma missing

(COM812)


92-92: Trailing comma missing

(COM812)


96-96: Trailing comma missing

(COM812)


120-120: Trailing comma missing

(COM812)


132-132: Trailing comma missing

(COM812)


141-141: Trailing comma missing

(COM812)


163-163: Trailing comma missing

(COM812)


167-167: Trailing comma missing

(COM812)


191-191: Trailing comma missing

(COM812)


203-203: Trailing comma missing

(COM812)


213-213: Trailing comma missing

(COM812)


235-235: Trailing comma missing

(COM812)


259-259: Trailing comma missing

(COM812)


275-275: Trailing comma missing

(COM812)


294-294: Trailing comma missing

(COM812)


310-310: Trailing comma missing

(COM812)

🔇 Additional comments (2)
README.md (1)

65-72: LGTM!

The changelog entries correctly document the new Langchain instrumentation features introduced in versions 3.8.4 and 3.8.5.

maxim/logger/langchain/instrumenter.py (1)

385-427: Well-designed instrumentation implementation!

The patching strategy is comprehensive and handles:

  • Both sync and async method variants
  • Optional dependencies gracefully
  • Multiple LangChain components (LLMs, chains, agents, tools, retrievers)

The use of lambda with default argument capture on line 401 is a clever way to handle the async flag.

@@ -1,4 +1,5 @@
from .tracer import MaximLangchainTracer
from .instrumenter import instrument_langchain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the syntax error in instrumenter.py before importing.

The import will fail due to a syntax error in instrumenter.py at line 42. The docstring needs proper indentation.

Also applies to: 36-36

🧰 Tools
🪛 Pylint (3.3.7)

[error] 2-2: Cannot import 'instrumenter' due to 'expected an indented block after function definition on line 41 (maxim.logger.langchain.instrumenter, line 42)'

(E0001)

🤖 Prompt for AI Agents
In maxim/logger/langchain/instrumenter.py at line 42 and also line 36, fix the
syntax error caused by improper indentation of the docstring. Ensure the
docstring is correctly indented according to Python standards so that the file
can be imported without syntax errors in __init__.py at line 2.

Comment on lines +65 to +208
def _wrap_llm(method: Callable, async_fn: bool) -> Callable:
if async_fn:
async def async_wrapper(self, *args, **kwargs):
messages = args[0] if args else kwargs.get("messages")
metadata = kwargs.get("config", {}).get("metadata") or kwargs.get(
"metadata"
)
maxim_meta = metadata.get("maxim") if isinstance(metadata, dict) else None
trace_id = maxim_meta.get("trace_id") if maxim_meta else None
gen_name = maxim_meta.get("generation_name") if maxim_meta else None

is_local = trace_id is None and current_trace() is None
final_trace = trace_id or (current_trace().id if current_trace() else str(uuid4()))
trace = current_trace() or logger.trace(TraceConfig(id=final_trace))
generation = None
try:
model, params = parse_langchain_model_parameters(**kwargs)
provider = parse_langchain_provider({"name": self.__class__.__name__})
parsed_messages = parse_langchain_messages(messages) if messages is not None else []
generation = trace.generation(
GenerationConfig(
id=str(uuid4()),
provider=provider,
model=model,
name=gen_name,
model_parameters=params,
messages=parsed_messages,
)
)
except Exception as e: # pragma: no cover - best effort
scribe().warning(
f"[MaximSDK] Failed preparing langchain generation: {e}"
)

try:
result = await method(self, *args, **kwargs)
except Exception as err:
if generation is not None:
try:
generation.error(parse_langchain_llm_error(err))
except Exception:
pass
if is_local:
trace.end()
raise

try:
if generation is not None:
from langchain_core.messages import BaseMessage
from langchain_core.outputs import LLMResult

if isinstance(result, LLMResult):
generation.result(parse_langchain_llm_result(result))
elif isinstance(result, BaseMessage):
generation.result(
parse_base_message_to_maxim_generation(result)
)
else:
generation.result(result)
if is_local:
if isinstance(result, str):
trace.set_output(result)
elif hasattr(result, "content"):
trace.set_output(getattr(result, "content", ""))
trace.end()
except Exception as e: # pragma: no cover - best effort
scribe().warning(
f"[MaximSDK] Failed logging langchain generation: {e}"
)
return result

return async_wrapper
else:
def sync_wrapper(self, *args, **kwargs):
messages = args[0] if args else kwargs.get("messages")
metadata = kwargs.get("config", {}).get("metadata") or kwargs.get(
"metadata"
)
maxim_meta = metadata.get("maxim") if isinstance(metadata, dict) else None
trace_id = maxim_meta.get("trace_id") if maxim_meta else None
gen_name = maxim_meta.get("generation_name") if maxim_meta else None

is_local = trace_id is None and current_trace() is None
final_trace = trace_id or (current_trace().id if current_trace() else str(uuid4()))
trace = current_trace() or logger.trace(TraceConfig(id=final_trace))
generation = None
try:
model, params = parse_langchain_model_parameters(**kwargs)
provider = parse_langchain_provider({"name": self.__class__.__name__})
parsed_messages = parse_langchain_messages(messages) if messages is not None else []
generation = trace.generation(
GenerationConfig(
id=str(uuid4()),
provider=provider,
model=model,
name=gen_name,
model_parameters=params,
messages=parsed_messages,
)
)
except Exception as e: # pragma: no cover - best effort
scribe().warning(
f"[MaximSDK] Failed preparing langchain generation: {e}"
)

try:
result = method(self, *args, **kwargs)
except Exception as err:
if generation is not None:
try:
generation.error(parse_langchain_llm_error(err))
except Exception:
pass
if is_local:
trace.end()
raise

try:
if generation is not None:
from langchain_core.messages import BaseMessage
from langchain_core.outputs import LLMResult

if isinstance(result, LLMResult):
generation.result(parse_langchain_llm_result(result))
elif isinstance(result, BaseMessage):
generation.result(
parse_base_message_to_maxim_generation(result)
)
else:
generation.result(result)
if is_local:
if isinstance(result, str):
trace.set_output(result)
elif hasattr(result, "content"):
trace.set_output(getattr(result, "content", ""))
trace.end()
except Exception as e: # pragma: no cover - best effort
scribe().warning(
f"[MaximSDK] Failed logging langchain generation: {e}"
)
return result

return sync_wrapper

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider adding trailing commas for better diff readability.

While not critical, adding trailing commas to multi-line function calls improves maintainability and creates cleaner diffs when parameters are added or removed.

Example locations that could benefit from trailing commas:

  • Line 70: After "metadata"
  • Line 92: After the closing )
  • Line 141: After "metadata"
  • Line 163: After the closing )
🧰 Tools
🪛 Ruff (0.11.9)

70-70: Trailing comma missing

(COM812)


92-92: Trailing comma missing

(COM812)


96-96: Trailing comma missing

(COM812)


120-120: Trailing comma missing

(COM812)


132-132: Trailing comma missing

(COM812)


141-141: Trailing comma missing

(COM812)


163-163: Trailing comma missing

(COM812)


167-167: Trailing comma missing

(COM812)


191-191: Trailing comma missing

(COM812)


203-203: Trailing comma missing

(COM812)

🤖 Prompt for AI Agents
In maxim/logger/langchain/instrumenter.py between lines 65 and 208, add trailing
commas to multi-line function calls and dictionary literals, specifically after
"metadata" on lines 70 and 141, and after the closing parentheses on lines 92
and 163. This involves placing a comma at the end of these lines to improve diff
readability and maintainability without changing functionality.

Comment on lines +41 to +48
def instrument_langchain(logger: Logger) -> None:
"""Instrument LangChain classes for automatic tracing.
The helper monkey-patches core LangChain classes so that LLM calls are
recorded as Maxim generations and chain or agent executions become spans.
If no trace is active, a temporary trace is created around the call so logs
are always captured.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix the critical syntax error in the docstring.

The docstring is not properly indented, causing a syntax error that prevents the module from being imported.

Apply this fix:

 def instrument_langchain(logger: Logger) -> None:
-"""Instrument LangChain classes for automatic tracing.
-
-    The helper monkey-patches core LangChain classes so that LLM calls are
-    recorded as Maxim generations and chain or agent executions become spans.
-    If no trace is active, a temporary trace is created around the call so logs
-    are always captured.
-    """
+    """Instrument LangChain classes for automatic tracing.
+
+    The helper monkey-patches core LangChain classes so that LLM calls are
+    recorded as Maxim generations and chain or agent executions become spans.
+    If no trace is active, a temporary trace is created around the call so logs
+    are always captured.
+    """
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def instrument_langchain(logger: Logger) -> None:
"""Instrument LangChain classes for automatic tracing.
The helper monkey-patches core LangChain classes so that LLM calls are
recorded as Maxim generations and chain or agent executions become spans.
If no trace is active, a temporary trace is created around the call so logs
are always captured.
"""
def instrument_langchain(logger: Logger) -> None:
"""Instrument LangChain classes for automatic tracing.
The helper monkey-patches core LangChain classes so that LLM calls are
recorded as Maxim generations and chain or agent executions become spans.
If no trace is active, a temporary trace is created around the call so logs
are always captured.
"""
🧰 Tools
🪛 Ruff (0.11.9)

42-42: SyntaxError: Expected an indented block after function definition

🪛 Pylint (3.3.7)

[error] 42-42: Parsing failed: 'expected an indented block after function definition on line 41 (maxim.logger.langchain.instrumenter, line 42)'

(E0001)

🤖 Prompt for AI Agents
In maxim/logger/langchain/instrumenter.py around lines 41 to 48, the docstring
for the function instrument_langchain is not properly indented, causing a syntax
error. Fix this by indenting all lines of the docstring to align with the
function body, ensuring the triple quotes and all text inside the docstring are
consistently indented one level inside the function definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants