Skip to main content
GeminiProvider wraps the google-genai SDK to connect Logicore to Google’s Gemini family of models. Gemini is natively multimodal — text, images, and function calls are first-class inputs — and supports some of the largest context windows available (up to 2M tokens on Gemini 1.5 Pro).

Installation

1

Install Python dependencies

pip install logicore google-genai
2

Set your API key

export GEMINI_API_KEY=AIza...
On Windows:
set GEMINI_API_KEY=AIza...
Get a free key at aistudio.google.com.

Constructor parameters

from logicore.providers.gemini_provider import GeminiProvider

provider = GeminiProvider(model_name="gemini-1.5-flash")
model_name
string
required
The Gemini model ID to use. Examples: "gemini-1.5-flash", "gemini-1.5-pro", "gemini-2.5-pro", "gemini-2.0-flash". Check the Google AI model catalog for the current list.
api_key
string
Your Gemini API key. If omitted, the provider reads GEMINI_API_KEY then GOOGLE_API_KEY from the environment. Raises ValueError if neither is found.

Basic usage

import asyncio
from logicore.agents.agent import Agent
from logicore.providers.gemini_provider import GeminiProvider

async def main():
    provider = GeminiProvider(model_name="gemini-1.5-flash")

    agent = Agent(
        llm=provider,
        role="Multimodal Assistant",
        system_message="Answer accurately and briefly."
    )

    result = await agent.chat("Explain retrieval augmented generation in 4 lines.")
    print(result)

asyncio.run(main())
System prompts passed via system_message are automatically mapped to Gemini’s system_instruction field.

Streaming

GeminiProvider runs the Gemini streaming API in a thread executor to keep the asyncio event loop unblocked:
import asyncio
from logicore.providers.gemini_provider import GeminiProvider

async def main():
    provider = GeminiProvider(model_name="gemini-1.5-flash")

    async def on_token(token: str):
        print(token, end="", flush=True)

    result = await provider.chat_stream(
        messages=[
            {"role": "user", "content": "Write a haiku about distributed systems."}
        ],
        on_token=on_token
    )

    print()
    print("Role:", result["role"])

asyncio.run(main())
chat_stream returns a dict with role, content, and optionally tool_calls once all tokens have been received.

Tool calling

Logicore converts standard tool definitions to Gemini FunctionDeclaration objects automatically:
def fetch_doc_title(url: str) -> str:
    """Fetch the page title for a URL."""
    return f"Title for {url}"

agent = Agent(
    llm=GeminiProvider(model_name="gemini-1.5-flash"),
    tools=[fetch_doc_title]
)

result = await agent.chat("What is the title of https://docs.logicore.ai?")
print(result)
Function call IDs are formatted as call_<function_name> and tool results are returned to Gemini as FunctionResponse parts in the conversation history.

Vision / multimodal

All Gemini models support images natively. Pass image parts alongside text:
import asyncio
from logicore.agents.agent import Agent
from logicore.providers.gemini_provider import GeminiProvider

async def main():
    agent = Agent(
        llm=GeminiProvider(model_name="gemini-2.5-pro"),
        role="Vision Assistant"
    )

    message = [
        {"type": "text", "text": "Describe this image in one sentence."},
        {"type": "image_url", "image_url": "/path/to/diagram.png"}
    ]

    result = await agent.chat(message)
    print(result)

asyncio.run(main())
Supported image_url values:
  • Local file path
  • https:// image URL
  • data:image/...;base64,... inline data
For documents, charts, or screenshots requiring detailed analysis, gemini-1.5-pro and gemini-2.5-pro deliver the best accuracy. For quick image classification or captioning, gemini-1.5-flash is faster and cheaper.

How Gemini-specific formatting works

The GeminiGateway handles several format differences transparently:
ConcernHow it is handled
System messagesExtracted and passed as system_instruction in GenerateContentConfig
Tool definitionsConverted to FunctionDeclaration objects in a Tool config
Tool resultsReturned as Content(role="tool", parts=[FunctionResponse(...)])
Assistant roleMapped to Gemini’s "model" role in Content objects

Troubleshooting

Neither api_key nor GEMINI_API_KEY/GOOGLE_API_KEY is set. Get a free key at aistudio.google.com and export it.
The model may have triggered a safety filter or the prompt produced no output. Try rephrasing the request or check the Gemini safety settings for your API key.
You are passing an image to a model or API version that does not support vision. Ensure you are using gemini-1.5-flash, gemini-1.5-pro, gemini-2.0-flash, or gemini-2.5-pro.
Gemini requires tool responses to be returned as FunctionResponse parts, not plain text. Logicore handles this automatically when tools are registered on Agent. If you are calling provider.chat() directly, ensure your conversation history follows the Gemini format (role "tool" with the correct name field).
The free tier has per-minute and per-day limits. Upgrade to a pay-as-you-go plan in the Google Cloud Console or add exponential back-off in your application code.

Build docs developers (and LLMs) love