Support

How the SDK was built, what broke, and how to fix it. If something is missing, open an issue.

Install

pip install palmshed-sdk

Optional: pip install palmshed-sdk[redis] for caching, palmshed-sdk[vertex] for Vertex AI image generation.

Environment

GEMINI_API_KEY        Required. Your Gemini API key.
REDIS_URL             Optional. Redis URL for caching.
IMAGE_PROVIDER        Optional. gemini, vertex, or mock.
IMAGE_MODEL           Optional. Image model name.
GOOGLE_CLOUD_PROJECT  Optional. GCP project for Vertex AI.
GOOGLE_CLOUD_LOCATION Optional. GCP location for Vertex AI.

Why this exists

The SDK started inside the Alma project. It was a module called palmshed_ai/sdk.py that lived inside a Flask application. It had hardcoded model names, a Go service dependency, and was coupled to Flask's request lifecycle.

Other Palmshed products needed the same Gemini interface. Nuntius needed it for PR reviews. Future products would need it too. Extracting it into a standalone package was the right thing to do.

The extraction

Four files moved from backend/palmshed_ai/ to palmshed_sdk/:

File What it does What changed
sdk.py GeminiAI class with all API methods Removed Flask coupling. Model names via ModelConfig. Go service URL optional.
models.py Model name constants Became a ModelConfig dataclass. No more module-level constants.
image_models.py Image config, result, and status types Moved as-is. No changes needed.
image_providers.py Gemini, Vertex, and Mock image providers Moved as-is. Registry works the same.

The error recovery problem

The Gemini SDK (google-genai) raises ValueError for rate limits. This is wrong. A rate limit is not a programming error. It is an API error that should be handled gracefully.

The first version of _is_recoverable_api_error returned False for ValueError. So every 429 rate limit became a 500 error. The user saw "Internal Server Error" with no explanation.

The fix: return True for all exceptions. Then refine. TypeError, NameError, AttributeError, KeyError, NotImplementedError are programming errors. Everything else is an API error that can fall back to synthetic text.

@staticmethod
def _is_recoverable_api_error(e: Exception) -> bool:
    if isinstance(e, (
        TypeError, ValueError, NameError,
        AttributeError, KeyError, NotImplementedError,
    )):
        return False
    return True

The empty response problem

The Gemini SDK returns a response object with a text property. Sometimes text is an empty string. The SDK does not raise. It returns successfully with nothing.

Every method in the SDK now checks for empty response.text. If empty, it returns a synthetic placeholder instead of failing silently. This is better than returning nothing, but worse than a real response. The user should check their API quota if this happens consistently.

# Before: only checks for None
if response.text is None:
    return "No response generated."

# After: checks for None and empty string
if not response.text:
    return "Synthesized answer for ..."

Design decisions

Troubleshooting

Symptom Cause Resolution
ValueError: GEMINI_API_KEY is required No API key passed or in environment Set GEMINI_API_KEY or pass api_key= to constructor.
Empty responses from generate_text Gemini returned empty text SDK returns synthetic fallback. Check your API quota.
Redis connection error Redis not running or wrong URL SDK falls back to in-memory cache. No action needed.
Image generation fails Model does not support image output Use gemini-2.5-flash-image or set IMAGE_PROVIDER=mock for testing.
Research times out Deep Research agent took longer than 5 minutes Check your API key permissions for the Deep Research agent.
Vertex AI image generation fails google-cloud-aiplatform not installed Run pip install palmshed-sdk[vertex].

Using with Alma

from palmshed_sdk import GeminiAI

client = GeminiAI()  # reads GEMINI_API_KEY from env

# Chat
response = client.generate_chat([
    {"role": "user", "content": "Hello"}
])

# Thinking
result = client.generate_text_with_thinking("Explain this code")
print(result["response"])
print(result["thinking_summary"])

Using with nuntius

from palmshed_sdk import GeminiAI, ModelConfig

models = ModelConfig(
    text="gemini-2.5-flash",
    thinking="gemini-2.5-flash",
)

client = GeminiAI(api_key=key, models=models)

# Review a PR diff
response = client.generate_chat([
    {"role": "user", "content": f"Review this diff:\n{diff}"}
])

Found something unexpected? Report an issue.