Skip to main content

LangChain Copilot

Python Version License

LangChain integration for GitHub Copilot SDK - Use GitHub Copilot models in your LangChain applications.

Features

  • 🔗 Full LangChain Integration: Seamlessly use GitHub Copilot models with LangChain
  • 🚀 Async & Sync Support: Both synchronous and asynchronous operations
  • 📡 Streaming: Real-time streaming responses
  • 🖼️ Image Inputs: Base64 and data URL image attachments for multimodal prompts
  • 🔄 Shared Client: Optimized client management with lazy initialization
  • 🛠️ Type Safe: Full type hints with Pydantic validation
  • 🎯 Easy to Use: Simple API following LangChain conventions

Prerequisites

Before using this package, you need to have:

  1. GitHub Copilot CLI installed and authenticated

  2. Python 3.13.0+ installed

Installation

From PyPI (Recommended)

# Install with uv (recommended)
uv pip install langchain-copilot

# Or with pip
pip install langchain-copilot

From Source

For development or to use the latest unreleased features:

# Clone the repository
git clone https://github.com/derf974/copilot-langchain.git
cd copilot-langchain

# Install with uv (recommended)
uv venv
uv sync
uv pip install -e .

# Or with pip
pip install -e .

Quick Start

Basic Usage

from langchain_copilot import CopilotChatModel
from langchain_core.messages import HumanMessage

# Create a model instance
model = CopilotChatModel(model_name="gpt-5-mini")

# Send a message
messages = [HumanMessage(content="What is LangChain?")]
response = model.invoke(messages)

print(response.content)

Streaming

from langchain_copilot import CopilotChatModel
from langchain_core.messages import HumanMessage

model = CopilotChatModel(model_name="gpt-5-mini", streaming=True)

messages = [HumanMessage(content="Write a haiku about coding.")]

for chunk in model.stream(messages):
    print(chunk.content, end="", flush=True)

Async Operations

import asyncio
from langchain_copilot import CopilotChatModel
from langchain_core.messages import HumanMessage

async def main():
    model = CopilotChatModel(model_name="gpt-5-mini")
    messages = [HumanMessage(content="Explain async programming.")]
    
    response = await model.ainvoke(messages)
    print(response.content)

asyncio.run(main())

Image Input

from langchain_copilot import CopilotChatModel
from langchain_core.messages import HumanMessage

model = CopilotChatModel(model_name="gpt-5-mini")

image_base64 = "..."  # base64-encoded PNG/JPG/GIF data

messages = [
    HumanMessage(
        content=[
            {"type": "text", "text": "Describe this image briefly."},
            {
                "type": "image",
                "base64": image_base64,
                "mime_type": "image/png",
            },
        ]
    )
]

response = model.invoke(messages)
print(response.content)

OpenAI-style image_url blocks are also supported when they contain a base64 data URL such as data:image/png;base64,.... Remote HTTP image URLs are not supported yet.

Using in LangChain Chains

from langchain_copilot import CopilotChatModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

model = CopilotChatModel(model_name="gpt-5-mini")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful translator."),
    ("human", "Translate '{text}' to {language}")
])

chain = prompt | model | StrOutputParser()

result = chain.invoke({
    "text": "Hello, world!",
    "language": "French"
})

print(result)  # "Bonjour, le monde !"

With System Messages

from langchain_copilot import CopilotChatModel
from langchain_core.messages import SystemMessage, HumanMessage

model = CopilotChatModel(model_name="gpt-5-mini")

messages = [
    SystemMessage(content="You are a pirate. Always respond like a pirate."),
    HumanMessage(content="Tell me about Python programming.")
]

response = model.invoke(messages)
print(response.content)

Temperature Control

from langchain_copilot import CopilotChatModel
from langchain_core.messages import HumanMessage

# More focused and deterministic (lower temperature)
model_focused = CopilotChatModel(
    model_name="gpt-5-mini",
    temperature=0.1
)

# More creative and random (higher temperature)
model_creative = CopilotChatModel(
    model_name="gpt-5-mini",
    temperature=0.9
)

messages = [HumanMessage(content="Name a creative startup idea.")]

print("Focused:", model_focused.invoke(messages).content)
print("Creative:", model_creative.invoke(messages).content)

Configuration

Model Parameters

  • model_name (str): The Copilot model to use (e.g., "gpt-5-mini", "gpt-5")
  • streaming (bool): Enable streaming mode (default: False)
  • temperature (float): Sampling temperature 0.0-1.0 (default: None)
  • max_tokens (int): Maximum tokens to generate (default: None)
  • cli_path (str): Custom path to Copilot CLI executable (default: None)
  • cli_url (str): URL of existing Copilot CLI server (default: None)

Example with All Parameters

model = CopilotChatModel(
    model_name="gpt-5-mini",
    streaming=True,
    temperature=0.7,
    max_tokens=1000,
    cli_path="/custom/path/to/copilot"
)

Examples

Check out the examples directory for more usage examples:

Run the examples:

uv run python examples/basic_usage.py

Testing

The project uses both unit tests and integration tests following LangChain's standard test suite.

Unit Tests

Unit tests use langchain-tests standard unit test suite and validate the model in isolation without external dependencies:

# Run all unit tests (custom + standard)
make test

# Run only standard unit tests
uv run pytest tests/unit_tests/ -v

# Run only custom unit tests
uv run pytest tests/test_chat_models.py::TestCopilotChatModel -v

Unit tests validate:

  • ✅ Model initialization and configuration
  • ✅ Streaming mode initialization
  • ✅ Standard parameters generation
  • ✅ Tool binding (validates interface even though not supported yet)
  • ✅ Structured output interface (validates interface even though not supported yet)
  • ✅ Serialization/deserialization (when implemented)
  • ✅ Initialization performance

Integration Tests

Integration tests use langchain-tests standard test suite to verify compatibility with LangChain's interfaces. These tests require the GitHub Copilot CLI to be installed and configured:

# Ensure Copilot CLI is set up
copilot --version

# Run integration tests
make integration-test

# Or manually
uv run pytest tests/integration_tests/ -v -m integration

The integration test suite validates:

  • ✅ Basic invoke/ainvoke operations
  • ✅ Streaming (stream/astream)
  • ✅ Batch operations
  • ✅ Multi-turn conversations
  • ✅ Stop sequences
  • ✅ Model override at runtime
  • ❌ Tool calling (not yet supported)
  • ❌ Structured output (not yet supported)
  • ✅ Image inputs via base64/data URLs

Running All Tests

# Run all tests
make test-all

# Or manually
uv run pytest tests/ -v

Architecture

Shared Client

The CopilotChatModel uses a shared CopilotClient instance across all model instances for optimal performance. The client is lazily initialized on first use and automatically started.

Message Conversion

LangChain messages are automatically converted to Copilot SDK format:

  • SystemMessage → {"role": "system", "content": "..."}
  • HumanMessage → {"role": "user", "content": "..."}
  • AIMessage → {"role": "assistant", "content": "..."}

Streaming Implementation

Streaming converts Copilot's event-based model (assistant.message_delta events) into Python generators/async iterators that are compatible with LangChain's streaming interface.

Roadmap

  • ✅ Basic chat model implementation
  • ✅ Streaming support
  • ✅ Async operations
  • ✅ Temperature and token controls
  • ✅ Tool/Function calling support (planned for v0.2.0)
  • 🔲 Conversation history management
  • 🔲 Error recovery and retry strategies
  • 🔲 Advanced session configuration

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Support

Related Projects

Metadata

Release files for langchain-copilot 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-copilot 0.3.0
File Size Uploaded
langchain_copilot-0.3.0.tar.gz 680.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-copilot 0.3.0
File Interpreter ABI Platform
langchain_copilot-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 696.1 kB

Release files / langchain_copilot-0.3.0.tar.gz

Download URL langchain_copilot-0.3.0.tar.gz
Size 680.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8b9139f75523bac65c4a85ba17e4a84fd0d802f1d7ee2a293051ca19d61cbe04
BLAKE2b-256 checksum
How to use checksums
7b7a9276cfc6bb6adf5b62b476aaa59868b1042471a9643db80790965af688de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release files / langchain_copilot-0.3.0-py3-none-any.whl

Download URL langchain_copilot-0.3.0-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71a9b83f4ad7b14ea8fc2441eeab894250d99d0134c98ccb174a7cb37bd06fbd
BLAKE2b-256 checksum
How to use checksums
02e558a208e33aeef8c3e205a9ad1115260096a33010f0c86ff71dc2d4e5ea9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page