Skip to content

Commit e1e4b2b

Browse files
committed
fix: add unit tests
1 parent 156e2b0 commit e1e4b2b

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

integrations/langchain/tests/unit_tests/test_chat_models.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,18 @@ def test_chat_model_bind_tolls_with_invalid_choices(llm: ChatDatabricks) -> None
601601

602602

603603
# Pydantic-based schema
604+
class Justification(BaseModel):
605+
"""A justification to the answer including step-by-step reasoning and supporting sources."""
606+
607+
reasoning: str = Field(description="Step-by-step reasoning.")
608+
sources: list[str] = Field(description="Supporting sources")
609+
610+
604611
class AnswerWithJustification(BaseModel):
605612
"""An answer to the user question along with justification for the answer."""
606613

607614
answer: str = Field(description="The answer to the user question.")
608-
justification: str = Field(description="The justification for the answer.")
615+
justification: Justification = Field(description="The justification for the answer.")
609616

610617

611618
# Raw JSON schema
@@ -620,9 +627,25 @@ class AnswerWithJustification(BaseModel):
620627
"description": "The answer to the user question.",
621628
},
622629
"justification": {
623-
"type": "string",
630+
"type": "object",
624631
"title": "Justification",
625632
"description": "The justification for the answer.",
633+
"properties": {
634+
"reasoning": {
635+
"type": "string",
636+
"title": "Reasoning",
637+
"description": "Step-by-step reasoning.",
638+
},
639+
"sources": {
640+
"type": "array",
641+
"title": "Sources",
642+
"description": "Supporting sources",
643+
"items": {
644+
"type": "string"
645+
},
646+
}
647+
},
648+
"required": ["reasoning", "sources"],
626649
},
627650
},
628651
"required": ["answer", "justification"],
@@ -641,7 +664,14 @@ def test_chat_model_with_structured_output(llm, schema, method: str):
641664
if method == "function_calling":
642665
assert bind["tool_choice"]["function"]["name"] == "AnswerWithJustification"
643666
elif method == "json_schema":
644-
assert bind["response_format"]["json_schema"]["schema"] == JSON_SCHEMA
667+
print(bind)
668+
js = bind["response_format"]["json_schema"]
669+
assert js["name"] == "AnswerWithJustification"
670+
assert js["strict"] is True
671+
assert js["schema"]["additionalProperties"] is False
672+
assert js["schema"]["justification"]["properties"]["additionalProperties"] is False
673+
assert set(js["schema"]["required"]) == set(js["schema"]["properties"].keys())
674+
assert set(js["schema"]["justification"]["required"]) == set(js["schema"]["justification"]["properties"].keys())
645675
else:
646676
assert bind["response_format"] == {"type": "json_object"}
647677

0 commit comments

Comments
 (0)