Skip to main content

LangExtract provider plugin for OpenRouter API

Project description

LangExtract OpenRouter Provider

A LangExtract provider plugin for OpenRouter.

Installation

# Using uv (recommended)
uv pip install langextract-provider-openrouter

# Or using pip
pip install langextract-provider-openrouter

Quick Start

  1. Set your OpenRouter API key:

    export OPENROUTER_API_KEY=your_key_here
    
  2. Use it in LangExtract:

    import langextract as lx
    from langextract import data
    
    # LangExtract requires at least one example
    examples = [
        data.ExampleData(
            text="John Doe works at Google",
            extractions=[
                data.Extraction(extraction_class="Person", extraction_text="John Doe"),
                data.Extraction(extraction_class="Company", extraction_text="Google")
            ]
        )
    ]
    
    # Use the 'openrouter/' prefix followed by the OpenRouter model ID
    result = lx.extract(
        text_or_documents="Alice Smith joined Apple",
        prompt_description="Extract people and companies",
        examples=examples,
        model_id="openrouter/google/gemini-2.0-flash-exp:free"
    )
    
    for ext in result.extractions:
        print(f"{ext.extraction_class}: {ext.extraction_text}")
    

Supported Models

This provider supports any model available on OpenRouter by prefixing the model ID with openrouter/. Common examples include:

  • openrouter/google/gemini-3-flash-preview
  • openrouter/google/gemini-3-pro-preview
  • openrouter/google/gemini-2.5-flash-preview-09-2025
  • openrouter/Qwen/Qwen2.5-VL-72B-Instruct
  • openrouter/openai/gpt-5

Configuration Options

Customize your requests using the provider_options dictionary.

Reasoning Effort

Control the reasoning effort for models that support it.

# Create a model with high reasoning effort
config = factory.ModelConfig(
    model_id="openrouter/Qwen/Qwen2.5-VL-72B-Instruct",
    provider_kwargs={
        "provider_options": {
            "effort": "high"
        }
    }
)

Token Usage

The provider logs token usage (prompt, completion, and total) to the console and standard logs (INFO level) after each request.

[OpenRouter] Token Usage - Prompt: 229, Completion: 127, Total: 356

Routing/Provider Options

These options control how OpenRouter acts as a router:

  • sort: Strategy for provider selection ("price", "latency", "throughput"). Default: "price".
  • effort: Reasoning effort level for reasoning models ("minimal", "low", "medium", "high"). Default: "minimal".
  • ignore: List of providers to ignore (e.g. ["OpenAI"]).
  • allow_fallbacks: Boolean to allow falling back to other providers. Default: True.
  • data_collection: "allow" or "deny". Default: "allow".

Usage Example (Inference Override)

result = lx.extract(
    # ... inputs ...
    # Example: GPT-5
    model_id="openrouter/openai/gpt-5",
    provider_options={
        "effort": "medium",
        "sort": "throughput",
        "ignore": ["SlowProvider"]
    }
)

Development

cd langextract-openrouter
uv venv --python 3.12
# Install dependencies
uv pip install -e ../langextract
uv pip install -e .
uv pip install pytest requests
# Run tests
uv run pytest

Complete Example

For a complete, runnable example, see example_usage.py in the root directory.

import langextract as lx
from langextract import data

examples = [
    data.ExampleData(
        text="John Doe works at Google",
        extractions=[
            data.Extraction(extraction_class="Person", extraction_text="John Doe"),
            data.Extraction(extraction_class="Company", extraction_text="Google")
        ]
    )
]

result = lx.extract(
    "Alice Smith joined Apple", 
    "Extract people and companies", 
    examples=examples,
    model_id="openrouter/google/gemini-2.0-flash-exp:free"
)
for ext in result.extractions:
    print(f"{ext.extraction_class}: {ext.extraction_text}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langextract_provider_openrouter-0.1.3.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file langextract_provider_openrouter-0.1.3.tar.gz.

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3fd6f64395b3ba7b5bcda5954066c8c4c4bad0764f7b56acb1764717f967636b
MD5 f1bf09095ba23d24df6cfe99bf70f01e
BLAKE2b-256 3b3942ca0a291d91d2bb9ce3327ae013cc6a51eafda7debea0a50403c4dd640c

See more details on using hashes here.

File details

Details for the file langextract_provider_openrouter-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d9cadb54187de247245a31e56d2718a68f1991fa85a8f51bd5d4a461497ab7c3
MD5 a3cec7403acc2e5468d20179120aa25d
BLAKE2b-256 7827868e72150d234597c5a3d518cfaebab6b5b976dcff99c9c04aed2548e42b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page