Skip to content

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

@jimkring

Description

@jimkring

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions