test_structured_few_shot_examples() — langchain Function Reference
Architecture documentation for the test_structured_few_shot_examples() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 18fdc68e_c165_9372_e436_588a330986a6["test_structured_few_shot_examples()"] 971e928f_9c9b_ce7a_b93d_e762f2f5aa54["ChatModelIntegrationTests"] 18fdc68e_c165_9372_e436_588a330986a6 -->|defined in| 971e928f_9c9b_ce7a_b93d_e762f2f5aa54 style 18fdc68e_c165_9372_e436_588a330986a6 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/standard-tests/langchain_tests/integration_tests/chat_models.py lines 2093–2153
def test_structured_few_shot_examples(
self, model: BaseChatModel, my_adder_tool: BaseTool
) -> None:
"""Test that the model can process few-shot examples with tool calls.
These are represented as a sequence of messages of the following form:
- `HumanMessage` with string content;
- `AIMessage` with the `tool_calls` attribute populated;
- `ToolMessage` with string content;
- `AIMessage` with string content (an answer);
- `HumanMessage` with string content (a follow-up question).
This test should be skipped if the model does not support tool calling
(see configuration below).
??? note "Configuration"
To disable tool calling tests, set `has_tool_calling` to `False` in your
test class:
```python
class TestMyChatModelIntegration(ChatModelIntegrationTests):
@property
def has_tool_calling(self) -> bool:
return False
```
??? question "Troubleshooting"
If this test fails, check that the model can correctly handle this
sequence of messages.
You can `xfail` the test if tool calling is implemented but this format
is not supported.
```python
@pytest.mark.xfail(reason=("Not implemented."))
def test_structured_few_shot_examples(self, *args: Any) -> None:
super().test_structured_few_shot_examples(*args)
```
"""
if not self.has_tool_calling:
pytest.skip("Test requires tool calling.")
model_with_tools = model.bind_tools([my_adder_tool], tool_choice="any")
function_result = json.dumps({"result": 3})
tool_schema = my_adder_tool.args_schema
assert isinstance(tool_schema, type)
assert issubclass(tool_schema, BaseModel)
few_shot_messages = tool_example_to_messages(
"What is 1 + 2",
[tool_schema(a=1, b=2)],
tool_outputs=[function_result],
ai_response=function_result,
)
messages = [*few_shot_messages, HumanMessage("What is 3 + 4")]
result = model_with_tools.invoke(messages)
assert isinstance(result, AIMessage)
Domain
Subdomains
Source
Frequently Asked Questions
What does test_structured_few_shot_examples() do?
test_structured_few_shot_examples() is a function in the langchain codebase, defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py.
Where is test_structured_few_shot_examples() defined?
test_structured_few_shot_examples() is defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py at line 2093.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free