Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Dec 16, 2024

Important

Handle optional serialized values gracefully in langchain.py and extract_model.py by updating functions to accept None and adding error handling.

  • Behavior:
    • Update get_langchain_run_name() in langchain.py to handle None for serialized argument, using try-except blocks for key access.
    • Modify _extract_model_name() in extract_model.py to accept None for serialized, adjusting logic to prevent errors.
  • Functions:
    • Change serialized parameter type from Dict[str, Any] to Optional[Dict[str, Any]] in multiple functions in langchain.py and extract_model.py, including on_chain_start(), on_llm_start(), and _extract_model_by_path_for_id().
    • Add checks for None in _extract_model_from_repr_by_pattern() and _extract_model_by_path() in extract_model.py.
  • Misc:
    • Update docstrings in langchain.py to reflect changes in parameter types and behavior.

This description was created by Ellipsis for b1a3ce6. It will automatically update as commits are pushed.

* Fix runnable name determination in LangChain integration

Issue #3578:
LangChain made the `serialized` parameter of some callback handler methods optional. The case where `serialized` is `None` has to be considered in the determination of the name of a LangChain runnable.

Changes:
- The implementation of `langfuse.callback.langchain.LangchainCallbackHandler.get_langchain_run_name` is fixed.
- The docstring of `langfuse.callback.langchain.LangchainCallbackHandler.get_langchain_run_name` is slightly changed and adapted.

* Fix type hints of various serialized parameters

Issue: (Reviewer request in PR #1033)
LangChain made the `serialized` parameter of some callback handler methods optional in the sense that they sometimes pass `None`. Although LangChain still uses the wrong (non-optional) type hint, we already want to use the correct one and handle the case `None` in langfuse.extract_model.

Changes:
- The type hints of the various `serialized` parameters in langfuse.callback.langchain and langfuse.extract_model are made optional.
- Handling of the case `None` is added to langfuse.extract_model.
- The type hint of the `select_from` parameters in helper functions of langfuse.extract_model is fixed.
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Disclaimer: Experimental PR review

PR Summary

This PR improves error handling and type safety for optional serialized values in the Langchain integration of langfuse-python.

  • Fixed unsafe dictionary access in extract_model.py by adding proper null checks for serialized parameter
  • Added proper type hints for optional parameters in langchain.py callback handler methods
  • Refactored get_langchain_run_name to use try/except blocks instead of dict.get() for safer error handling
  • Improved handling of potentially missing keys in AzureOpenAI model extraction logic
  • Added documentation clarifying optional parameter behavior in callback methods

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@hassiebp hassiebp merged commit 2324237 into main Dec 16, 2024
9 of 10 checks passed
@hassiebp hassiebp deleted the fix-langchain-optional-serialized branch December 16, 2024 13:38
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.

3 participants