mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 18:50:33 +00:00
openai[patch]: accept json schema response format directly (#27623)
fix #25460 --------- Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -23,6 +23,7 @@ from langchain_openai import ChatOpenAI
|
||||
from langchain_openai.chat_models.base import (
|
||||
_convert_dict_to_message,
|
||||
_convert_message_to_dict,
|
||||
_convert_to_openai_response_format,
|
||||
_create_usage_metadata,
|
||||
_format_message_content,
|
||||
)
|
||||
@@ -761,3 +762,46 @@ def test__create_usage_metadata() -> None:
|
||||
input_token_details={},
|
||||
output_token_details={},
|
||||
)
|
||||
|
||||
|
||||
def test__convert_to_openai_response_format() -> None:
|
||||
# Test response formats that aren't tool-like.
|
||||
response_format: dict = {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": "math_reasoning",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"steps": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"explanation": {"type": "string"},
|
||||
"output": {"type": "string"},
|
||||
},
|
||||
"required": ["explanation", "output"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
"final_answer": {"type": "string"},
|
||||
},
|
||||
"required": ["steps", "final_answer"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
"strict": True,
|
||||
},
|
||||
}
|
||||
|
||||
actual = _convert_to_openai_response_format(response_format)
|
||||
assert actual == response_format
|
||||
|
||||
actual = _convert_to_openai_response_format(response_format["json_schema"])
|
||||
assert actual == response_format
|
||||
|
||||
actual = _convert_to_openai_response_format(response_format, strict=True)
|
||||
assert actual == response_format
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
_convert_to_openai_response_format(response_format, strict=False)
|
||||
|
||||
Reference in New Issue
Block a user