Skip to content

feat(core): option with type narrowing #31945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: v04_standard_outputs
Choose a base branch
from

Conversation

eyurtsev
Copy link
Collaborator

@eyurtsev eyurtsev commented Jul 9, 2025

We need to look at a few more options


run time cast makes it behave like old code:

                block = cast("dict[str, Any]", block)
                if block.get("type") == "text":
                    all_contents.append(block["text"])

type narrowing allows making it more typing friendly

def is_reasoning_block(block: Mapping[str, Any]) -> TypeGuard[ReasoningContentBlock]:
    """Check if a block is a ReasoningContentBlock."""
    return block.get("type") == "reasoning"


def is_text_block(block: Mapping[str, Any]) -> TypeGuard[TextContentBlock]:
    """Check if a block is a TextContentBlock."""
    return block.get("type") == "text"


def test_typing() -> None:
    """Test typing on things"""
    message = AIMessage(
        content="Hello",
    )
    if isinstance(message.content, str):
        # This should not raise an error
        message.content = message.content + " world"
    elif isinstance(message.content, list):
        all_contents = []
        for block in message.content:
            if isinstance(block, dict):
                if is_text_block(block):
                    all_contents.append(block["text"])
                if is_reasoning_block(block):
                    all_contents.append(block.get("reasoning", "foo"))

Copy link

vercel bot commented Jul 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jul 28, 2025 10:57pm

Copy link

codspeed-hq bot commented Jul 9, 2025

CodSpeed WallTime Performance Report

Merging #31945 will not alter performance

Comparing eugene/add_some_refactors (2276392) with v04_standard_outputs (5de65de)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

Copy link

codspeed-hq bot commented Jul 9, 2025

CodSpeed Instrumentation Performance Report

Merging #31945 will not alter performance

Comparing eugene/add_some_refactors (2276392) with v04_standard_outputs (5de65de)

Summary

✅ 11 untouched benchmarks
⁉️ 3 dropped benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
⁉️ test_stream_time 15.9 ms N/A N/A
⁉️ test_init_time 3.7 ms N/A N/A
⁉️ test_init_time_with_client 5.2 ms N/A N/A

@ccurme ccurme changed the base branch from master to v04_standard_outputs July 9, 2025 22:03
@mdrxy mdrxy changed the title option with type narrowing feat(core): option with type narrowing Jul 16, 2025
@mdrxy mdrxy added the core Related to the package `langchain-core` label Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the package `langchain-core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants