Skip to content
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

fix: make baml_py work with playwright/inspect #1214

Merged
merged 5 commits into from
Dec 5, 2024
Merged

Conversation

sxlijin
Copy link
Collaborator

@sxlijin sxlijin commented Dec 4, 2024

When using baml_py.Image in a Pydantic model with playwright-python (the headless browser stack), the playwright library freezes up (see user report). Specifically this code suffices to repro the issue:

from playwright.sync_api import sync_playwright
import baml_py
from pydantic import BaseModel

class Foo(BaseModel):
    screenshot: baml_py.Image

print('This happens')
sync_playwright().start()
print('This never happens')

The reason turns out to be that when the playwright context manager is entered, deep in the callstack, the playwright connection grabs the stack context using inspect.stack(). Because of how __get_pydantic_core_schema__ was implemented for baml_py.Image and baml_py.Audio, declaring a pydantic model that relied on either of these types would cause inspect.stack() to crash:

============================================================================= FAILURES ==============================================================================
___________________________________________________________________________ test_inspect ____________________________________________________________________________

    def test_inspect():
        class LoremIpsum(pydantic.BaseModel):
            my_image: baml_py.Image

>       inspect.stack()

tests/test_pydantic.py:88:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:1673: in stack
    return getouterframes(sys._getframe(1), context)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:1650: in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:1624: in getframeinfo
    lines, lnum = findsource(frame)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:952: in findsource
    module = getmodule(object, file)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:875: in getmodule
    f = getabsfile(module)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:844: in getabsfile
    _filename = getsourcefile(object) or getfile(object)
../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:817: in getsourcefile
    filename = getfile(object)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

object = <module 'rust:media_repr' from '/Users/sam/baml/integ-tests/python'>

    def getfile(object):
        """Work out which source or compiled file an object was defined in."""
        if ismodule(object):
            if getattr(object, '__file__', None):
                return object.__file__
>           raise TypeError('{!r} is a built-in module'.format(object))
E           TypeError: <module 'rust:media_repr' from '/Users/sam/baml/integ-tests/python'> is a built-in module

../../../.local/share/mise/installs/python/3.10.14/lib/python3.10/inspect.py:778: TypeError
====================================================================== short test summary info ======================================================================
FAILED tests/test_pydantic.py::test_inspect - TypeError: <module 'rust:media_repr' from '/Users/sam/baml/integ-tests/python'> is a built-in module

The fix turns out to be very simple: when we evaluate Python code in __get_pydantic_core_schema__, we just need to actually synthesize a file and module name for the evaluated code, instead of passing in an empty string (which is what we do today).

(Also, since we added pickle support for image/audio during the investigation, keep it.)


Important

Fixes inspect.stack() crash and adds pickle support for baml_py.Image and baml_py.Audio, with updated tests and CI workflow.

  • Behavior:
    • Fixes inspect.stack() crash by providing file and module name in __get_pydantic_core_schema__ for baml_py.Image and baml_py.Audio.
    • Adds pickle support for baml_py.Image and baml_py.Audio with py_new and __getnewargs__ methods.
  • Testing:
    • Adds test_inspect and test_pickle in test_python.py to verify compatibility and pickle functionality.
    • Introduces run_tests.sh for running Python integration tests.
  • CI/CD:
    • Updates primary.yml to include a scheduled job and integration tests job.
    • Adds poetry version to .mise.toml.

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

Copy link

vercel bot commented Dec 4, 2024

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

Name Status Preview Comments Updated (UTC)
baml ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 4, 2024 11:32pm

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

❌ Changes requested. Reviewed everything up to ade3902 in 1 minute and 36 seconds

More details
  • Looked at 329 lines of code in 11 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 drafted comments based on config settings.
1. .github/workflows/primary.yml:109
  • Draft comment:
    The version for 'actions/checkout' should be 'v3', not 'v4'.
  • Reason this comment was not posted:
    Comment did not seem useful.
2. engine/baml-runtime/src/tracing/mod.rs:202
  • Draft comment:
    Remove commented-out code to keep the codebase clean and maintainable. This applies to other instances of commented-out code in this file as well.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The PR includes commented-out code in multiple places. While it's common to leave comments for future reference, it's generally a best practice to remove commented-out code to keep the codebase clean and maintainable.
3. integ-tests/python/tests/test_pydantic.py:99
  • Draft comment:
    Consider adding assertions to verify the expected behavior of the inspect module, such as checking that the stack is not empty or contains expected values.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable:
    The test_inspect function is a regression test for a specific bug related to inspect.stack(). The current test checks for unexpected exceptions, which is a valid test for the described bug. Adding assertions about the stack's contents may not be necessary for this specific regression test.
    The comment might be suggesting a more thorough test, but it could be unnecessary for the specific bug being tested. The current test seems sufficient for its purpose.
    The test is focused on ensuring no exceptions are raised, which aligns with the described bug. Additional assertions might not add value for this specific case.
    The comment is not necessary as the current test is sufficient for the regression bug it addresses. The comment should be deleted.

Workflow ID: wflow_q8AsMeWpJd0cRFdW


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

fn test_inspect() {
assert_eq!(
crate::MODULE_NAME,
format!("baml_py.{}", stringify!(baml_asdfpy))
Copy link
Contributor

Choose a reason for hiding this comment

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

The module name in the assertion should be consistent with the actual module name used in the code.

@sxlijin sxlijin changed the title fix: make our pyo3 play nice with inspect.stack() fix: make baml_client work with playwright Dec 4, 2024
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

❌ Changes requested. Incremental review on 951d1e7 in 51 seconds

More details
  • Looked at 217 lines of code in 7 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 drafted comments based on config settings.
1. engine/language_client_python/src/lib.rs:54
  • Draft comment:
    The LoremIpsum class and its methods have been removed. Ensure that no other parts of the codebase depend on this class, as its removal might cause runtime errors if referenced elsewhere.
  • Reason this comment was not posted:
    Comment did not seem useful.
2. engine/language_client_python/src/lib.rs:76
  • Draft comment:
    The test_inspect function is commented out. If this test is no longer needed, consider removing it to keep the code clean. If it is needed, uncomment and ensure it passes.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The test function test_inspect is commented out. If it's not needed, it should be removed to keep the code clean.
3. engine/language_client_python/src/types/audio.rs:57
  • Draft comment:
    Ensure that baml_deserialize correctly handles the data parameter and that it is safe from any potential deserialization vulnerabilities.
  • Reason this comment was not posted:
    Comment did not seem useful.
4. engine/language_client_python/src/types/image.rs:56
  • Draft comment:
    Ensure that baml_deserialize correctly handles the data parameter and that it is safe from any potential deserialization vulnerabilities.
  • Reason this comment was not posted:
    Marked as duplicate.

Workflow ID: wflow_jsX6dRvJxyshsHlE


Want Ellipsis to fix these issues? Tag @ellipsis-dev in a comment. You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on 1026360 in 11 seconds

More details
  • Looked at 20 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. .github/workflows/primary.yml:120
  • Draft comment:
    Ensure that poetry install is necessary by verifying that poetry is used in the project. The addition in the workflow aligns with the .mise.toml configuration.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The addition of poetry install in the workflow is necessary due to the addition of poetry in the .mise.toml file. This ensures that the Python dependencies are installed before running the tests.

Workflow ID: wflow_340eSfgUR5AV3QQN


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

@BoundaryML BoundaryML deleted a comment from ellipsis-dev bot Dec 4, 2024
@sxlijin sxlijin changed the title fix: make baml_client work with playwright fix: make baml_client work with playwright/inspect Dec 4, 2024
@sxlijin sxlijin changed the title fix: make baml_client work with playwright/inspect fix: make baml_py work with playwright/inspect Dec 4, 2024
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

👍 Looks good to me! Incremental review on bb55aa2 in 12 seconds

More details
  • Looked at 25 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 1 drafted comments based on config settings.
1. .github/workflows/primary.yml:16
  • Draft comment:
    Ensure the cron schedule 0 10 * * * is intended to run daily at 2am PST (10am UTC).
  • Reason this comment was not posted:
    Confidence changes required: 33%
    The cron schedule is set to run daily at 2am PST, which is 10am UTC. This is correctly set up for a daily schedule, but it's important to ensure that this is the intended frequency and time.

Workflow ID: wflow_G7p9iJE1nh5Yabvb


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

@sxlijin sxlijin added this pull request to the merge queue Dec 5, 2024
Merged via the queue into canary with commit 6741999 Dec 5, 2024
11 checks passed
@sxlijin sxlijin deleted the sam/freeze-fix branch December 5, 2024 01:02
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.

1 participant