# Python

Build AI applications with Python using the Graphlit SDK.

{% hint style="info" %}
**New to Graphlit?** Complete the [Quickstart tutorial](/getting-started/quickstart.md) for a hands-on introduction.
{% endhint %}

***

## Installation

Install the Graphlit client with pip:

```bash
pip install graphlit-client
```

**Requirements:**

* Python 3.8 or higher
* Graphlit account with [API credentials](/account-setup-one-time/create-project.md)

***

## Quick Start

```python
import asyncio
import os
from graphlit import Graphlit
from graphlit_api import *

async def main():
    # Reads from environment variables automatically
    graphlit = Graphlit()
    
    # Ingest content
    response = await graphlit.client.ingest_text(
        name="Product Requirements",
        text="Our AI agent needs persistent memory across sessions..."
    )
    
    print(f"✅ Memory created: {response.ingest_text.id}")

asyncio.run(main())
```

{% hint style="success" %}
**That's it!** The SDK automatically reads `GRAPHLIT_ORGANIZATION_ID`, `GRAPHLIT_ENVIRONMENT_ID`, and `GRAPHLIT_JWT_SECRET` from your environment.
{% endhint %}

***

## Configuration

### Environment Variables (Production)

Create a `.env` file (never commit this):

```bash
GRAPHLIT_ORGANIZATION_ID=your_actual_org_id
GRAPHLIT_ENVIRONMENT_ID=your_actual_env_id
GRAPHLIT_JWT_SECRET=your_actual_jwt_secret
```

Load it with python-dotenv:

```python
import asyncio
from dotenv import load_dotenv
from graphlit import Graphlit

load_dotenv()  # Loads .env file

async def main():
    graphlit = Graphlit()  # Reads from environment
    # Your code here
    
asyncio.run(main())
```

Install python-dotenv:

```bash
pip install python-dotenv
```

{% hint style="warning" %}
**Security:** Add `.env` to your `.gitignore` immediately. Use platform secrets (AWS Secrets Manager, etc.) in production deployments.
{% endhint %}

### Alternative: Explicit Configuration

Only use if you need to override environment variables:

```python
from graphlit import Graphlit
import os

graphlit = Graphlit(
    organization_id=os.environ['GRAPHLIT_ORGANIZATION_ID'],
    environment_id=os.environ['GRAPHLIT_ENVIRONMENT_ID'],
    jwt_secret=os.environ['GRAPHLIT_JWT_SECRET']
)
```

***

## Common Patterns

### Ingest Content

```python
# From URL
response = await graphlit.client.ingest_uri(
    uri="https://example.com/document.pdf"
)

# From text
response = await graphlit.client.ingest_text(
    name="Meeting Notes",
    text="Discussion about Q4 planning..."
)
```

### Search Memory

```python
response = await graphlit.client.query_contents(
    filter=ContentFilter(
        search="quarterly planning"
    )
)

for content in response.contents.results:
    print(f"📄 {content.name}")
```

### Chat with Context

```python
# Create conversation
conversation = await graphlit.client.create_conversation(
    conversation=ConversationInput(
        name="AI Assistant"
    )
)

# Ask questions
response = await graphlit.client.prompt_conversation(
    prompt="What did we discuss about Q4 planning?",
    id=conversation.create_conversation.id
)

print(response.prompt_conversation.message.message)
```

***

## Next Steps

**Quickstarts:**

* [Quickstart: Your First Agent](/getting-started/quickstart.md) - Build a streaming agent in 7 minutes
* [AI Agents](/tutorials/ai-agents.md) - Create agents with persistent memory
* [Knowledge Graph](/tutorials/knowledge-graph.md) - Extract entities and relationships

**Examples:**

* [Python Notebooks](https://github.com/graphlit/graphlit-samples/tree/main/python/Notebook%20Examples) - 60+ working examples
* [Streamlit Apps](https://github.com/graphlit/graphlit-samples/tree/main/python/Streamlit) - Full UI applications

**Resources:**

* [Python SDK on GitHub](https://github.com/graphlit/graphlit-client-python)
* [Use Case Library](/api-guides/use-cases.md) - 100+ code examples
* [Ask Graphlit](/resources/ask-graphlit.md) - AI code assistant
* [Join Discord](https://discord.gg/ygFmfjy3Qx) - Get help from the community


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graphlit.dev/sdk-setup/for-python-developers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
