test_structured_output() — langchain Function Reference
Architecture documentation for the test_structured_output() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 108c2749_d1e8_3b47_311a_c5c2ed3608b6["test_structured_output()"] 971e928f_9c9b_ce7a_b93d_e762f2f5aa54["ChatModelIntegrationTests"] 108c2749_d1e8_3b47_311a_c5c2ed3608b6 -->|defined in| 971e928f_9c9b_ce7a_b93d_e762f2f5aa54 4edc076f_69be_49a9_19b6_71533096c780["_get_joke_class()"] 108c2749_d1e8_3b47_311a_c5c2ed3608b6 -->|calls| 4edc076f_69be_49a9_19b6_71533096c780 style 108c2749_d1e8_3b47_311a_c5c2ed3608b6 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/standard-tests/langchain_tests/integration_tests/chat_models.py lines 2156–2237
def test_structured_output(
self,
model: BaseChatModel,
schema_type: Literal["pydantic", "typeddict", "json_schema"],
) -> None:
"""Test to verify structured output is generated both on invoke and stream.
This test is optional and should be skipped if the model does not support
structured output (see configuration below).
??? note "Configuration"
To disable structured output tests, set `has_structured_output` to `False`
in your test class:
```python
class TestMyChatModelIntegration(ChatModelIntegrationTests):
@property
def has_structured_output(self) -> bool:
return False
```
By default, `has_structured_output` is `True` if a model overrides the
`with_structured_output` or `bind_tools` methods.
??? question "Troubleshooting"
If this test fails, ensure that the model's `bind_tools` method
properly handles both JSON Schema and Pydantic V2 models.
`langchain_core` implements a [utility function](https://reference.langchain.com/python/langchain_core/utils/?h=convert_to_op#langchain_core.utils.function_calling.convert_to_openai_tool).
that will accommodate most formats.
See [example implementation](https://github.com/langchain-ai/langchain/blob/master/libs/partners/openai/langchain_openai/chat_models/base.py).
of `with_structured_output`.
"""
if not self.has_structured_output:
pytest.skip("Test requires structured output.")
schema, validation_function = _get_joke_class(schema_type)
chat = model.with_structured_output(schema, **self.structured_output_kwargs)
mock_callback = MagicMock()
mock_callback.on_chat_model_start = MagicMock()
invoke_callback = _TestCallbackHandler()
result = chat.invoke(
"Tell me a joke about cats.", config={"callbacks": [invoke_callback]}
)
validation_function(result)
assert len(invoke_callback.options) == 1, (
"Expected on_chat_model_start to be called once"
)
assert isinstance(invoke_callback.options[0], dict)
assert isinstance(
invoke_callback.options[0]["ls_structured_output_format"]["schema"], dict
)
assert invoke_callback.options[0]["ls_structured_output_format"][
"schema"
] == convert_to_json_schema(schema)
stream_callback = _TestCallbackHandler()
chunk = None
for chunk in chat.stream(
"Tell me a joke about cats.", config={"callbacks": [stream_callback]}
):
validation_function(chunk)
assert chunk is not None, "Stream returned no chunks - possible API issue"
assert len(stream_callback.options) == 1, (
"Expected on_chat_model_start to be called once"
)
assert isinstance(stream_callback.options[0], dict)
assert isinstance(
stream_callback.options[0]["ls_structured_output_format"]["schema"], dict
)
assert stream_callback.options[0]["ls_structured_output_format"][
"schema"
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does test_structured_output() do?
test_structured_output() is a function in the langchain codebase, defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py.
Where is test_structured_output() defined?
test_structured_output() is defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py at line 2156.
What does test_structured_output() call?
test_structured_output() calls 1 function(s): _get_joke_class.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free