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".
  • ignore: List of provider names to exclude (e.g., ["Parasail"]).
  • 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.2.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.2.tar.gz.

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 067b4ae9fbf6b8ee86469d3d2077a9b2c71ce4e8c734093fb07876c0669f7938
MD5 d5aa13b532dc4414e87ff7171eaaafe4
BLAKE2b-256 3f4ae9d4b210931b0db5be9f85588ac95356acc60e7a0859687a2f1db26594e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 384bdb7ed98cd729ec5fd97deeac56d9e151114964c595e0f8ccc5cde0e5c4e4
MD5 b33bed24abadb14b88d40fa48e779a42
BLAKE2b-256 9c70acf0c3b28f2c24b2801c8d0d91d96165347bd4dca1cf8bd23fc7670004b4

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