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

When using convert_to_openai_function, if a type hint of field is an Enum, its description will be lost. #26819

Open
5 tasks done
gbaian10 opened this issue Sep 24, 2024 · 2 comments
Assignees
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core

Comments

@gbaian10
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from enum import StrEnum

from langchain_core.utils.function_calling import convert_to_openai_function
from pydantic import BaseModel, Field


class WithDesciptionModel(BaseModel):
    foo: str = Field(..., description="This is a foo field")


class MyEnum(StrEnum):
    NAME = "name"
    AGE = "age"


class WithoutDesciptionModel(BaseModel):
    foo: MyEnum = Field(..., description="This is a foo field")


# The description of foo will be kept.
print(convert_to_openai_function(WithDesciptionModel))
# The description of foo will be lost.
print(convert_to_openai_function(WithoutDesciptionModel))


# print(WithDesciptionModel.model_json_schema())
# print(WithoutDesciptionModel.model_json_schema())

Error Message and Stack Trace (if applicable)

{'name': 'WithDesciptionModel', 'description': '', 'parameters': {'properties': {'foo': {'description': 'This is a foo field', 'type': 'string'}}, 'required': ['foo'], 'type': 'object'}}
{'name': 'WithoutDesciptionModel', 'description': '', 'parameters': {'properties': {'foo': {'enum': ['name', 'age'], 'type': 'string'}}, 'required': ['foo'], 'type': 'object'}}

Description

If the type hint in a BaseModel includes an Enum, its description will be lost.

System Info

langchain-core==0.3.5
pydantic==2.9.2

python-version==3.12.6

@dosubot dosubot bot added Ɑ: core Related to langchain-core 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Sep 24, 2024
@eyurtsev
Copy link
Collaborator

@gbaian10 do you use this together with the open ai chat model? did this arise while using with_structured_output

@eyurtsev eyurtsev self-assigned this Sep 24, 2024
@gbaian10
Copy link
Contributor Author

gbaian10 commented Sep 24, 2024

@eyurtsev Using with_structured_output also has this issue.
Although it's currently known that modifying the __doc__ of the Enum class itself can solve it, this doesn't seem like a good approach.

from enum import StrEnum, auto

from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

load_dotenv()


class NameEnum(StrEnum):
    ALICE = auto()
    BOB = auto()


class NewNameEnum(StrEnum):
    CHARLIE = auto()
    DAVID = auto()


description = "Name of the person"
NewNameEnum.__doc__ = description


class EnumModel(BaseModel):
    name: NameEnum = Field(..., description=description)


class StrModel(BaseModel):
    name: str = Field(..., description=description)


class FixEnumModel(BaseModel):
    name: NewNameEnum


inputs = "Hello, I am Alice."
model = ChatOpenAI(model="gpt-4o-mini")
model.with_structured_output(EnumModel).invoke(inputs)
model.with_structured_output(StrModel).invoke(inputs)
model.with_structured_output(FixEnumModel).invoke(inputs)

image
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: core Related to langchain-core
Projects
None yet
Development

No branches or pull requests

2 participants