Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,9 @@ class AnswerWithJustification(BaseModel):
response_format = {
"type": "json_schema",
"json_schema": {
"name": kwargs.get("schema_name", "json_schema"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name should be derived and not hardcoded - here you should use convert_to_openai_function() ,(add an additional import from the langchain core function calling class for from langchain_core.utils.function_calling import convert_to_openai_function . we should also do this to address the other cascading errors (see my overall review comment)

can refer to how the partner library is doing this as a reference which calls convert_to_openai_function:
https://github.com/langchain-ai/langchain/blob/202d7f6c4a2ca8c7e5949d935bcf0ba9b0c23fb0/libs/partners/openai/langchain_openai/chat_models/base.py#L1449

suggested fix may look something like this, but please be sure to test!

from langchain_core.utils.function_calling import (
      convert_to_openai_function,  # add this import
      convert_to_openai_tool,
  )

  # ... in with_structured_output, json_schema branch:

  elif method == "json_schema":
      if schema is None:
          raise ValueError(
              "schema must be specified when method is 'json_schema'. Received None."
          )
      function = convert_to_openai_function(
          pydantic_schema if pydantic_schema else schema, strict=True
      )
      function["schema"] = function.pop("parameters")
      response_format = {"type": "json_schema", "json_schema": function}
      llm = self.bind(response_format=response_format)

"strict": True,
"schema": (
schema.model_json_schema() if is_pydantic_schema else schema # type: ignore[union-attr]
),
"schema": schema.model_json_schema() if is_pydantic_schema else schema, # type: ignore[union-attr]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pr is also missing unit test coverage - we have an existing test at tests/unit_tests/test_chat_models.py (test_chat_model_with_structured_output)

in which this line
assert bind["response_format"]["json_schema"]["schema"] == JSON_SCHEMA would likely fail.

can you update this test and include asserts for the name/strict/schema fields? may look something like:

elif method == "json_schema":
      js = bind["response_format"]["json_schema"]
      assert js["name"] == "AnswerWithJustification"
      assert js["strict"] is True
      assert js["schema"]["additionalProperties"] is False

can you also add the follow unit tests:

  • pydantic schema produces a valid response_format
  • raw dict schema produces a valid response_format
  • nested Pydantic model to validate $ref dereferencing?

}
llm = self.bind(response_format=response_format)
Expand Down