-
Notifications
You must be signed in to change notification settings - Fork 15.9k
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
langchain.core : Use shallow copy for schema manipulation in JsonOutputParser.get_format_instructions #17162
langchain.core : Use shallow copy for schema manipulation in JsonOutputParser.get_format_instructions #17162
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a comment about why this is neccessary, and a quick unit test
9696c9c
to
4583715
Compare
@hwchase17 class Joke(BaseModel):
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")
joke_query = "Tell me a joke."
parser = JsonOutputParser(pydantic_object=Joke)
prompt = PromptTemplate(
template="Answer the user query.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": parser.get_format_instructions()}, #cause
)
chain = prompt | model | parser
chain.invoke({"query": joke_query})
openai_functions = [convert_to_openai_function(Joke)]
parser = JsonOutputFunctionsParser()
chain2 = prompt | model.bind(functions=openai_functions) | parser
chain2.invoke({"query": "tell me a joke"}) In this code, schema = self.pydantic_object.schema()
reduced_schema = schema
if "title" in reduced_schema:
del reduced_schema["title"]
if "type" in reduced_schema:
del reduced_schema["type"] The use of BaseModel's schema_str = json.dumps(reduced_schema)
return JSON_FORMAT_INSTRUCTIONS.format(schema=schema_str) Please let me know if there is anything that needs clarification, if there's anything I've missed, or if the test code is inadequate or lacking :) |
c11fd8f
to
400f10a
Compare
400f10a
to
9d3a9ec
Compare
9d3a9ec
to
8a2c1db
Compare
ec1f05e
to
f18c31b
Compare
@@ -221,7 +221,7 @@ def get_format_instructions(self) -> str: | |||
if self.pydantic_object is None: | |||
return "Return a JSON object." | |||
else: | |||
schema = self.pydantic_object.schema() | |||
schema = {k: v for k, v in self.pydantic_object.schema().items()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a comment here would be great! otherwise looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hwchase17
I've applied the changes, thank you :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have also made the same changes in /langchain/langchain/output_parsers/yaml.py
and /langchain/langchain/output_parsers/pydantic.py
.
Fix: Use shallow copy for schema manipulation in get_format_instructions
Prevents side effects on the original schema object by using a dictionary comprehension for a safer and more controlled manipulation of schema key-value pairs, enhancing code reliability.