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

from __future__ import annotations causes ai functions to return strings #950

Open
3 tasks done
jimkring opened this issue Jul 14, 2024 · 5 comments
Open
3 tasks done
Labels
bug Something isn't working

Comments

@jimkring
Copy link
Contributor

First check

  • I added a descriptive title to this issue.
  • I used the GitHub search to try to find a similar issue and didn't find one.
  • I searched the Marvin documentation for this issue.

Bug summary

I was having a strange issue where ai functions were returning string data instead of the type specified in my function signature. It turns out that having from __future__ import annotations was causing the issue.

Reproduction

from __future__ import annotations  # this causes the problem to appear
import inspect  # we'll use this to look at why it's happening 

import marvin
from pydantic import BaseModel

@marvin.fn
def recipe(
        ingredients: list[str],
        max_cook_time: int = 15,
        cuisine: str = "North Italy",
        experience_level: str = "beginner",
) -> Recipe:
    """
    Returns a complete recipe that uses all the `ingredients` and
    takes less than `max_cook_time`  minutes to prepare. Takes 
    `cuisine` style and the chef's `experience_level` into account 
    as well.
    """

# get a recipe
my_recipe = recipe(["milk", "sugar", "flour"])

# verify the output is a Recipe
assert isinstance(my_recipe, Recipe)

# let's check the function signature
sig = inspect.signature(recipe)

# make sure it's output is a Recipe
assert type(sig.return_annotation) is Recipe

Error

No response

Versions

> marvin version
Version:                2.3.6
Python version:         3.11.4
OS/Arch:                windows/amd64

Additional context

No response

@jimkring jimkring added the bug Something isn't working label Jul 14, 2024
@zzstoatzz
Copy link
Collaborator

hi @jimkring - thanks for the issue!

can you show the definition of the Recipe type?

@jimkring
Copy link
Contributor Author

This is from the Marvin examples. https://www.askmarvin.ai/docs/text/functions/#parameters


class Recipe(BaseModel):
    name: str
    cook_time_minutes: int
    ingredients: list[str]
    steps: list[str]

@jimkring
Copy link
Contributor Author

@zzstoatzz lmk if this is enough info for you to reproduce. Thx.

@zzstoatzz
Copy link
Collaborator

hi @jimkring

thanks for the issue! i have reproduced, it looks like the import itself indeed causes this, which is interesting. will take a look as soon as I can

@jimkring
Copy link
Contributor Author

@zzstoatzz Thank you. I was able to work around this issue by keeping my model definitions separate (in a different module) from where I call marvin. So, if a fix is not easy, a suitable workaround might be to just generate an error if this condition is detected along with a message that helps point to a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants