Skip to content

Commit 5d3bb8b

Browse files
Add OpenRouter provider support to auto_client routing
- Add 'openrouter' to supported_providers list - Implement OpenRouter routing logic following DeepSeek pattern - Use OpenAI-compatible API with https://openrouter.ai/api/v1 base URL - Default to Mode.TOOLS with support for OPENROUTER_STRUCTURED_OUTPUTS and JSON modes - Handle OPENROUTER_API_KEY from environment or kwargs Co-Authored-By: Jason Liu <jason@jxnl.co>
1 parent d94ce1b commit 5d3bb8b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

instructor/auto_client.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"deepseek",
3232
"fireworks",
3333
"ollama",
34+
"openrouter",
3435
"xai",
3536
"litellm",
3637
]
@@ -946,6 +947,60 @@ def from_provider(
946947
)
947948
raise
948949

950+
elif provider == "openrouter":
951+
try:
952+
import openai
953+
from instructor import from_openai
954+
import os
955+
956+
# Get API key from kwargs or environment
957+
api_key = api_key or os.environ.get("OPENROUTER_API_KEY")
958+
959+
if not api_key:
960+
from .core.exceptions import ConfigurationError
961+
962+
raise ConfigurationError(
963+
"OPENROUTER_API_KEY is not set. "
964+
"Set it with `export OPENROUTER_API_KEY=<your-api-key>` or pass it as kwarg api_key=<your-api-key>"
965+
)
966+
967+
# OpenRouter uses OpenAI-compatible API
968+
base_url = kwargs.pop("base_url", "https://openrouter.ai/api/v1")
969+
970+
client = (
971+
openai.AsyncOpenAI(api_key=api_key, base_url=base_url)
972+
if async_client
973+
else openai.OpenAI(api_key=api_key, base_url=base_url)
974+
)
975+
976+
result = from_openai(
977+
client,
978+
model=model_name,
979+
mode=mode if mode else instructor.Mode.TOOLS,
980+
**kwargs,
981+
)
982+
logger.info(
983+
"Client initialized",
984+
extra={**provider_info, "status": "success"},
985+
)
986+
return result
987+
except ImportError:
988+
from .core.exceptions import ConfigurationError
989+
990+
raise ConfigurationError(
991+
"The openai package is required to use the OpenRouter provider. "
992+
"Install it with `pip install openai`."
993+
) from None
994+
except Exception as e:
995+
logger.error(
996+
"Error initializing %s client: %s",
997+
provider,
998+
e,
999+
exc_info=True,
1000+
extra={**provider_info, "status": "error"},
1001+
)
1002+
raise
1003+
9491004
elif provider == "litellm":
9501005
try:
9511006
from litellm import completion, acompletion

0 commit comments

Comments
 (0)