Skip to content

Incompatibility with OpenAIEmbeddings #29

@vladimirkovacevic

Description

@vladimirkovacevic

There is incompatibility with OpenAIEmbeddings. If model is used from OpenRouter API instead OpenAI, it outputs embeddings, but embeddings.embed_documents expects response object to have field "data" (langchain_openai/embeddings/base.py, line 585: embeddings.extend(r["embedding"] for r in response["data"])But, OpenRouter model returns string object (does not have "data" field) and the code crushes.

from langchain_openai import OpenAIEmbeddings

Make sure your OpenRouter API key is set

os.environ["OPENAI_API_KEY"] = "your_openrouter_api_key_here"
os.environ["OPENAI_BASE_URL"] = "https://openrouter.ai/api/v1"

embeddings = OpenAIEmbeddings(
model="text-embedding-3-small", # OpenRouter-supported embedding model
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ["OPENAI_BASE_URL"],
)

Embed all document chunks

mocked_input_texts = [doc.page_content for doc in texts]
vectors = embeddings.embed_documents(mocked_input_texts)

Hope this is OK place to put this issue. If not, please be free to move it elsewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions