The dataset is currently empty. Upload or create new data files. Then, you will be able to explore them in the Dataset Viewer.

YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

jshape + NexaAPI: Bulletproof JSON Handling for AI API Development

jshape just dropped on PyPI and it solves the #1 pain point of AI API development — malformed JSON responses. Here's how to use it with NexaAPI for bulletproof AI-powered applications.

NexaAPI PyPI jshape PyPI nexaapi

The Problem

Every AI developer has seen this:

# AI API returns malformed JSON
response = '{"name": "John", "age": 30, "city": "New York"'  # Missing closing brace
# json.loads(response)  # ❌ JSONDecodeError!

AI models sometimes return:

  • Missing closing braces/brackets
  • Trailing commas
  • Unquoted keys
  • Markdown code blocks wrapped around JSON
  • Partial responses due to token limits

The Solution: jshape

pip install jshape nexaapi
import jshape
from nexaapi import NexaAPI

client = NexaAPI(api_key='YOUR_API_KEY')  # Get free key at nexa-api.com

# Get AI response (might be malformed JSON)
response = client.chat.completions.create(
    model='gpt-4o',
    messages=[{
        "role": "user",
        "content": "Return a JSON object with user profile data"
    }]
)

raw_json = response.choices[0].message.content

# Fix malformed JSON with jshape
fixed_json = jshape.repair(raw_json)
data = jshape.loads(fixed_json)

print(data)  # ✅ Works even if AI returned malformed JSON

Real-World Example: Image Generation with JSON Config

import jshape
from nexaapi import NexaAPI
import json

client = NexaAPI(api_key='YOUR_API_KEY')

def generate_image_from_ai_config(user_request: str) -> str:
    """
    1. Ask AI to generate image config as JSON
    2. Fix any malformed JSON with jshape
    3. Generate image with NexaAPI
    """
    # Step 1: Get image config from AI
    config_response = client.chat.completions.create(
        model='gpt-4o',
        messages=[{
            "role": "user",
            "content": f"Generate a JSON config for this image: {user_request}. Include: prompt, width, height, style"
        }]
    )
    
    raw_config = config_response.choices[0].message.content
    
    # Step 2: Fix malformed JSON (jshape handles edge cases)
    config = jshape.loads(raw_config)
    
    # Step 3: Generate image with NexaAPI
    image = client.image.generate(
        model='stable-diffusion-xl',
        prompt=config['prompt'],
        width=config.get('width', 1024),
        height=config.get('height', 1024)
    )
    
    return image.image_url

# Usage
url = generate_image_from_ai_config("a futuristic city at sunset")
print(f"Generated image: {url}")

JavaScript Version

import NexaAPI from 'nexaapi'; // npm install nexaapi
import { repair, parse } from 'jshape'; // npm install jshape

const client = new NexaAPI({ apiKey: 'YOUR_API_KEY' });

async function generateWithAIConfig(userRequest) {
  // Get config from AI
  const configResponse = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: `Generate JSON config for: ${userRequest}` }]
  });
  
  const rawConfig = configResponse.choices[0].message.content;
  
  // Fix malformed JSON
  const config = parse(repair(rawConfig));
  
  // Generate image
  const image = await client.image.generate({
    model: 'stable-diffusion-xl',
    prompt: config.prompt,
    width: config.width || 1024,
    height: config.height || 1024
  });
  
  return image.imageUrl;
}

Links

Topics

jshape json-repair python ai-api nexaapi malformed-json developer-tools ai-development

Downloads last month
8