langchain.core : Use shallow copy for schema manipulation in JsonOutputParser.get_format_instructions (#17162)

- **Description :**  

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.

  - **Issue:**  #17161 
  - **Dependencies:** None
  -  **Twitter handle:** None
This commit is contained in:
JongRok BAEK
2024-02-14 06:30:53 +09:00
committed by GitHub
parent 90f55e6bd1
commit 8d6cc90fc5
4 changed files with 22 additions and 3 deletions

View File

@@ -29,7 +29,8 @@ class PydanticOutputParser(JsonOutputParser):
raise OutputParserException(msg, llm_output=json_object)
def get_format_instructions(self) -> str:
schema = self.pydantic_object.schema()
# Copy schema to avoid altering original Pydantic schema.
schema = {k: v for k, v in self.pydantic_object.schema().items()}
# Remove extraneous fields.
reduced_schema = schema

View File

@@ -43,7 +43,8 @@ class YamlOutputParser(BaseOutputParser[T]):
raise OutputParserException(msg, llm_output=text) from e
def get_format_instructions(self) -> str:
schema = self.pydantic_object.schema()
# Copy schema to avoid altering original Pydantic schema.
schema = {k: v for k, v in self.pydantic_object.schema().items()}
# Remove extraneous fields.
reduced_schema = schema