This commit is contained in:
Mason Daugherty
2026-02-13 00:24:03 -05:00
parent eca3a2d73e
commit 25b6308fbf
6 changed files with 2480 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,69 @@
"""Integration tests for `ChatOpenRouter` chat model."""
from __future__ import annotations
import pytest
from langchain_core.messages import AIMessageChunk, BaseMessageChunk
from pydantic import BaseModel, Field
from langchain_openrouter.chat_models import ChatOpenRouter
def test_basic_invoke() -> None:
"""Test basic invocation."""
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
response = model.invoke("Say 'hello' and nothing else.")
assert response.content
assert response.response_metadata.get("model_provider") == "openrouter"
def test_streaming() -> None:
"""Test streaming."""
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
full: BaseMessageChunk | None = None
for chunk in model.stream("Say 'hello' and nothing else."):
full = chunk if full is None else full + chunk
assert isinstance(full, AIMessageChunk)
assert full.content
def test_tool_calling() -> None:
"""Test tool calling via OpenRouter."""
class GetWeather(BaseModel):
"""Get the current weather in a given location."""
location: str = Field(description="The city and state")
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
model_with_tools = model.bind_tools([GetWeather])
response = model_with_tools.invoke("What's the weather in San Francisco?")
assert response.tool_calls
def test_structured_output() -> None:
"""Test structured output via OpenRouter."""
class Joke(BaseModel):
"""A joke."""
setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline of the joke")
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
structured = model.with_structured_output(Joke)
result = structured.invoke("Tell me a joke about programming")
assert isinstance(result, Joke)
assert result.setup
assert result.punchline
@pytest.mark.xfail(reason="Depends on reasoning model availability on OpenRouter.")
def test_reasoning_content() -> None:
"""Test reasoning content from a reasoning model."""
model = ChatOpenRouter(
model="openai/o3-mini",
openrouter_reasoning={"effort": "low"},
)
response = model.invoke("What is 2 + 2?")
assert response.content

View File

@@ -0,0 +1,39 @@
"""Standard integration tests for `ChatOpenRouter`."""
from langchain_tests.integration_tests import ChatModelIntegrationTests
from langchain_openrouter.chat_models import ChatOpenRouter
MODEL_NAME = "openai/gpt-4o-mini"
class TestChatOpenRouter(ChatModelIntegrationTests):
"""Test `ChatOpenRouter` chat model."""
@property
def chat_model_class(self) -> type[ChatOpenRouter]:
"""Return class of chat model being tested."""
return ChatOpenRouter
@property
def chat_model_params(self) -> dict:
"""Parameters to create chat model instance for testing."""
return {
"model": MODEL_NAME,
"temperature": 0,
}
@property
def supports_json_mode(self) -> bool:
"""Whether the chat model supports JSON mode."""
return True
@property
def supports_image_inputs(self) -> bool:
"""Whether the chat model supports image inputs."""
return True
@property
def supports_image_urls(self) -> bool:
"""Whether the chat model supports image inputs from URLs."""
return True

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
"""Test `langchain_openrouter` public API surface."""
from langchain_openrouter import __all__
EXPECTED_ALL = [
"ChatOpenRouter",
]
def test_all_imports() -> None:
"""Verify that __all__ exports match the expected public API."""
assert sorted(EXPECTED_ALL) == sorted(__all__)

View File

@@ -0,0 +1,39 @@
"""Standard unit tests for `ChatOpenRouter`."""
from langchain_tests.unit_tests import ChatModelUnitTests
from langchain_openrouter.chat_models import ChatOpenRouter
MODEL_NAME = "openai/gpt-4o-mini"
class TestChatOpenRouterUnit(ChatModelUnitTests):
"""Standard unit tests for `ChatOpenRouter` chat model."""
@property
def chat_model_class(self) -> type[ChatOpenRouter]:
"""Chat model class being tested."""
return ChatOpenRouter
@property
def init_from_env_params(self) -> tuple[dict, dict, dict]:
"""Parameters to initialize from environment variables."""
return (
{
"OPENROUTER_API_KEY": "api_key",
},
{
"model": MODEL_NAME,
},
{
"openrouter_api_key": "api_key",
},
)
@property
def chat_model_params(self) -> dict:
"""Parameters to create chat model instance for testing."""
return {
"model": MODEL_NAME,
"api_key": "test-api-key",
}