-
Notifications
You must be signed in to change notification settings - Fork 19.5k
fix(xai): inject model_provider in response_metadata
#33543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
955692a
tests(xai): rfc, expand reasoning tests, check for `model_provider`
mdrxy 32c453f
fix(xai): inject `model_provider` into `response_metadata`
mdrxy 978ad9d
fix(xai): update reasoning test to clarify model capabilities
mdrxy 6067b6b
fix: use `override` from `typing_extensions` for Python<3.12 compatib…
mdrxy fb14df7
Merge branch 'master' into mdrxy/xai-v1-tests
mdrxy 4bfe25d
resolve comments
mdrxy 3072424
Merge branch 'master' into mdrxy/xai-v1-tests
mdrxy bb26e7b
Merge branch 'master' into mdrxy/xai-v1-tests
mdrxy 25d6359
.
mdrxy ec6a40e
Merge branch 'mdrxy/xai-v1-tests' of github.com:langchain-ai/langchai…
mdrxy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
libs/partners/xai/tests/integration_tests/test_chat_models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """Integration tests for ChatXAI specific features.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Literal | ||
|
|
||
| import pytest | ||
| from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessageChunk | ||
|
|
||
| from langchain_xai import ChatXAI | ||
|
|
||
| MODEL_NAME = "grok-4-fast-reasoning" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("output_version", ["", "v1"]) | ||
| def test_reasoning(output_version: Literal["", "v1"]) -> None: | ||
| """Test reasoning features. | ||
|
|
||
| Note: `grok-4` does not return `reasoning_content`, but may optionally return | ||
| encrypted reasoning content if `use_encrypted_content` is set to True. | ||
| """ | ||
| # Test reasoning effort | ||
| if output_version: | ||
| chat_model = ChatXAI( | ||
| model="grok-3-mini", | ||
| reasoning_effort="low", | ||
| output_version=output_version, | ||
| ) | ||
| else: | ||
| chat_model = ChatXAI( | ||
| model="grok-3-mini", | ||
| reasoning_effort="low", | ||
| ) | ||
| input_message = "What is 3^3?" | ||
| response = chat_model.invoke(input_message) | ||
| assert response.content | ||
| assert response.additional_kwargs["reasoning_content"] | ||
|
|
||
| # Test streaming | ||
| full: BaseMessageChunk | None = None | ||
| for chunk in chat_model.stream(input_message): | ||
| full = chunk if full is None else full + chunk | ||
| assert isinstance(full, AIMessageChunk) | ||
| assert full.additional_kwargs["reasoning_content"] | ||
|
|
||
| # Check that we can access reasoning content blocks | ||
| assert response.content_blocks | ||
| reasoning_content = ( | ||
| block for block in response.content_blocks if block["type"] == "reasoning" | ||
| ) | ||
| assert len(list(reasoning_content)) >= 1 | ||
|
|
||
| # Test that passing message with reasoning back in works | ||
| follow_up_message = "Based on your reasoning, what is 4^4?" | ||
| followup = chat_model.invoke([input_message, response, follow_up_message]) | ||
| assert followup.content | ||
| assert followup.additional_kwargs["reasoning_content"] | ||
| followup_reasoning = ( | ||
| block for block in followup.content_blocks if block["type"] == "reasoning" | ||
| ) | ||
| assert len(list(followup_reasoning)) >= 1 | ||
|
|
||
| # Test passing in a ReasoningContentBlock | ||
| response_metadata = {"model_provider": "xai"} | ||
| if output_version: | ||
| response_metadata["output_version"] = output_version | ||
| msg_w_reasoning = AIMessage( | ||
| content_blocks=response.content_blocks, | ||
| response_metadata=response_metadata, | ||
| ) | ||
| followup_2 = chat_model.invoke( | ||
| [msg_w_reasoning, "Based on your reasoning, what is 5^5?"] | ||
| ) | ||
| assert followup_2.content | ||
| assert followup_2.additional_kwargs["reasoning_content"] | ||
|
|
||
|
|
||
| def test_web_search() -> None: | ||
| llm = ChatXAI( | ||
| model=MODEL_NAME, | ||
| search_parameters={"mode": "on", "max_search_results": 3}, | ||
| ) | ||
|
|
||
| # Test invoke | ||
| response = llm.invoke("Provide me a digest of world news in the last 24 hours.") | ||
| assert response.content | ||
| assert response.additional_kwargs["citations"] | ||
| assert len(response.additional_kwargs["citations"]) <= 3 | ||
|
|
||
| # Test streaming | ||
| full = None | ||
| for chunk in llm.stream("Provide me a digest of world news in the last 24 hours."): | ||
| full = chunk if full is None else full + chunk | ||
| assert isinstance(full, AIMessageChunk) | ||
| assert full.additional_kwargs["citations"] | ||
| assert len(full.additional_kwargs["citations"]) <= 3 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nonealso works for v0