# LangChain and LlamaIndex

Use SciSpace as a retriever so an existing RAG application gains scholarly grounding.

> [!info] Community adapters, not first-party packages
> There is no official SciSpace package for either framework. The recipes below use the
> Python SDK directly, which is stable and versioned. Roughly 30 lines gets you a working
> retriever, and you keep control of how citations flow through your chain.

## LangChain retriever

```python
from typing import List
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from scispace import Scispace

class SciSpaceRetriever(BaseRetriever):
    client: Scispace = Scispace()
    depth: str = "standard"
    max_papers: int = 20

    def _get_relevant_documents(self, query: str) -> List[Document]:
        search = self.client.searches.create(
            query=query, depth=self.depth, max_papers=self.max_papers
        ).wait()
        return [
            Document(
                page_content=c.quote or "",
                metadata={
                    "source_id": c.source.id,
                    "title": c.source.title,
                    "source_type": c.source.type,   # paper | document | model
                    "page": c.locations[0].page if c.locations else None,
                    "url": c.source.url,
                },
            )
            for c in search.answer.citations
        ]
```

## LlamaIndex

Wrap the same call in a `BaseRetriever` returning `NodeWithScore`, carrying the identical metadata
keys. The mapping is what matters, not the framework.

## Preserving citations through the chain

This is the whole point of the integration, and the easiest thing to lose.

- **Keep `source_id` and `page` on every node.** A chain that concatenates `page_content` into a prompt and discards metadata produces an ungrounded answer with extra steps.
- **Drop or label `source_type == "model"` nodes.** They carry no quote and no locator.
- **Render citations from metadata, not from the LLM's output.** Never let a downstream model invent citation numbers — map them from `source_id`.

## Cost

Each retriever call is one search: 5 credits at `standard`. A chain that retrieves on
every turn of a conversation multiplies that — cache by normalized query. See cost-control.

## Related

searches · grounded-answers · tool-use · python
