You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fromenumimportStrEnumfromlangchain_core.utils.function_callingimportconvert_to_openai_functionfrompydanticimportBaseModel, FieldclassWithDesciptionModel(BaseModel):
foo: str=Field(..., description="This is a foo field")
classMyEnum(StrEnum):
NAME="name"AGE="age"classWithoutDesciptionModel(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())
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
The text was updated successfully, but these errors were encountered:
dosubotbot
added
Ɑ: core
Related to langchain-core
🤖:bug
Related to a bug, vulnerability, unexpected error with an existing feature
labels
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.
fromenumimportStrEnum, autofromdotenvimportload_dotenvfromlangchain_openaiimportChatOpenAIfrompydanticimportBaseModel, Fieldload_dotenv()
classNameEnum(StrEnum):
ALICE=auto()
BOB=auto()
classNewNameEnum(StrEnum):
CHARLIE=auto()
DAVID=auto()
description="Name of the person"NewNameEnum.__doc__=descriptionclassEnumModel(BaseModel):
name: NameEnum=Field(..., description=description)
classStrModel(BaseModel):
name: str=Field(..., description=description)
classFixEnumModel(BaseModel):
name: NewNameEnuminputs="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)
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
If the type hint in a
BaseModel
includes anEnum
, its description will be lost.System Info
langchain-core==0.3.5
pydantic==2.9.2
python-version==3.12.6
The text was updated successfully, but these errors were encountered: