Skip to content

fix(langchain): support for date/time in langchain serializations - #2792

Merged
nirga merged 1 commit into
mainfrom
langchain-date
Mar 23, 2025
Merged

nirga merged 1 commit into
mainfrom
langchain-date

Conversation

@nirga

@nirga nirga commented Mar 22, 2025 •

Copy link
Copy Markdown
Member

Fixes #2775

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Important

Adds support for datetime serialization in Langchain and updates Python version to 3.11.

  • Behavior:
    • Adds support for datetime serialization in CallbackFilteredJSONEncoder in utils.py by converting datetime objects to ISO format.
    • Updates Python version to 3.11 in .python-version.
  • Testing:
    • Adds test_lcel_with_datetime in test_lcel.py to verify serialization of datetime in metadata.
    • Includes a new VCR cassette test_lcel_with_datetime.yaml for testing date/time serialization.

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

@ellipsis-dev ellipsis-dev Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 Looks good to me! Reviewed everything up to 9ff8d4f in 1 minute and 22 seconds

More details
  • Looked at 220 lines of code in 4 files
  • Skipped 0 files when reviewing.
  • Skipped posting 10 drafted comments based on config settings.
1. packages/opentelemetry-instrumentation-langchain/.python-version:1
  • Draft comment:
    Ensure the Python version bump to 3.11 is intentional and all dependencies are compatible.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50%
    This comment is asking the PR author to ensure compatibility of dependencies with the new Python version, which is against the rules. It also asks to confirm the intention of the version bump, which is not allowed.
2. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:28
  • Draft comment:
    The explicit datetime check returning isoformat is good. Consider if it's preferable to call super().default(o) rather than converting to string by default to preserve standard JSONEncoder behavior.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50%
    The change from super().default(o) to str(o) was intentional and adds more robustness by handling objects that can be string-converted but not JSON-serialized. The new code also adds proper error handling and logging when serialization fails. The original behavior of raising TypeError for non-serializable objects may be too strict for this use case. The custom encoder seems focused on being permissive and handling edge cases gracefully.
    The comment raises a valid point about standard JSON encoder behavior, and there might be cases where preserving that behavior is important for consistency.
    The new implementation appears to be an intentional design choice to be more permissive and handle errors gracefully, which is often desirable in instrumentation code that shouldn't break the main application.
    The comment should be deleted as the current implementation is an intentional improvement in error handling and flexibility, not a mistake.
3. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:31
  • Draft comment:
    Using a fallback of converting non-datetime objects to a string might mask serialization issues. Consider whether this is the intended behavior or if falling back to super().default(o) would be more appropriate.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50%
    The comment raises a valid point about masking serialization issues. However, the current implementation seems intentionally designed to be lenient - it tries to convert to string, logs failures, and returns empty string rather than erroring. This appears to be a deliberate choice for robustness. The previous behavior of using super().default(o) would raise TypeError for unsupported types.
    I could be wrong about the intention - maybe strict serialization with errors is actually better here. The current approach could hide bugs.
    The code is in an instrumentation library where failing gracefully is typically preferred over throwing errors. The debug logging ensures issues aren't completely hidden.
    The comment should be deleted. The current lenient approach appears intentional and appropriate for an instrumentation library, with proper error logging in place.
4. packages/opentelemetry-instrumentation-langchain/tests/test_lcel.py:257
  • Draft comment:
    The new test 'test_lcel_with_datetime' correctly verifies that datetime metadata is serialized as an ISO formatted string.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50%
    None
5. packages/opentelemetry-instrumentation-langchain/.python-version:1
  • Draft comment:
    Updating Python from 3.9.5 to 3.11 is a breaking change for some users. Ensure documentation notes this version upgrade requirement.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50%
    This comment is about a dependency change, specifically a Python version upgrade. The rules specify not to comment on pure dependency changes or library versions. Therefore, this comment should be removed.
6. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:28
  • Draft comment:
    Good job handling datetime serialization. Consider also supporting datetime.date (and possibly time) if those types can appear in outputs.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50%
    The comment is about code that was just added in this PR. It's a reasonable suggestion since datetime.date objects are common and would likely need similar handling. However, we don't have evidence that date objects actually appear in the data being serialized. The comment is somewhat speculative - "if those types can appear in outputs".
    We don't have concrete evidence that datetime.date objects are actually used in this codebase. The fallback str() conversion might be sufficient for date objects.
    While we lack evidence of date objects being used, adding support is a simple change that follows the pattern established for datetime objects and would make the code more robust.
    The comment should be deleted because it's speculative - we don't have evidence that datetime.date support is actually needed. The fallback str() conversion would handle date objects adequately if they do appear.
7. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:31
  • Draft comment:
    Fallback to returning an empty string on serialization error may mask issues. Consider whether logging at a higher level or returning None might be more appropriate for debugging.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50%
    This is a JSON encoder class, which means its output needs to be JSON-compatible. Both "" and None (which becomes null) are valid JSON values. The current approach of returning "" is actually safer since it preserves the field structure while being falsy, whereas null might cause issues with consumers expecting a string. The debug log already captures the error for debugging purposes.
    I might be underestimating the debugging value of distinguishing between empty strings and serialization failures. Some consumers might handle null differently than empty string in a beneficial way.
    While null might provide slightly more information, the debug log already captures the exact failure case. The empty string approach is more conservative and less likely to cause issues with JSON consumers.
    The current approach of returning "" with debug logging is a reasonable design choice. The suggested change to None doesn't clearly improve the situation and could potentially cause more problems.
8. packages/opentelemetry-instrumentation-langchain/tests/test_lcel.py:297
  • Draft comment:
    Test for datetime serialization is clear and validates the expected ISO string format. Ensure additional date/time types (if supported later) are also covered in tests.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50%
    The comment is asking the author to ensure future support for additional date/time types in tests. This is a speculative and non-specific request, which violates the rule against asking the author to ensure something is tested without being specific.
9. packages/opentelemetry-instrumentation-langchain/.python-version:1
  • Draft comment:
    There is no trailing newline at the end of this file. It is a minor stylistic issue that can help with tool compatibility. Please add a newline at the end of the file.
  • Reason this comment was not posted:
    Comment was on unchanged code.
10. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:60
  • Draft comment:
    Typo detected: 'OpenLLMetry' should be corrected to 'OpenTelemetry' for consistency and clarity.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.

Workflow ID: wflow_0JSTXoDtH27CBaYA


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On purpose?

@nirga
nirga merged commit 86a7efe into main Mar 23, 2025
@nirga
nirga deleted the langchain-date branch March 23, 2025 08:09
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.

🐛 Bug Report: Langchain instrumentation does not end spans when output contains Date fields

2 participants