NexaAPI commited on
Commit ·
cd0d7d3
0
Parent(s):
jshape + NexaAPI: Bulletproof JSON handling for AI APIs
Browse files
README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# jshape + NexaAPI: Bulletproof JSON Handling for AI API Development
|
| 2 |
+
|
| 3 |
+
> 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.
|
| 4 |
+
|
| 5 |
+
[](https://nexa-api.com)
|
| 6 |
+
[](https://pypi.org/project/jshape/)
|
| 7 |
+
[](https://pypi.org/project/nexaapi/)
|
| 8 |
+
|
| 9 |
+
## The Problem
|
| 10 |
+
|
| 11 |
+
Every AI developer has seen this:
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
# AI API returns malformed JSON
|
| 15 |
+
response = '{"name": "John", "age": 30, "city": "New York"' # Missing closing brace
|
| 16 |
+
# json.loads(response) # ❌ JSONDecodeError!
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
AI models sometimes return:
|
| 20 |
+
- Missing closing braces/brackets
|
| 21 |
+
- Trailing commas
|
| 22 |
+
- Unquoted keys
|
| 23 |
+
- Markdown code blocks wrapped around JSON
|
| 24 |
+
- Partial responses due to token limits
|
| 25 |
+
|
| 26 |
+
## The Solution: jshape
|
| 27 |
+
|
| 28 |
+
```bash
|
| 29 |
+
pip install jshape nexaapi
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
```python
|
| 33 |
+
import jshape
|
| 34 |
+
from nexaapi import NexaAPI
|
| 35 |
+
|
| 36 |
+
client = NexaAPI(api_key='YOUR_API_KEY') # Get free key at nexa-api.com
|
| 37 |
+
|
| 38 |
+
# Get AI response (might be malformed JSON)
|
| 39 |
+
response = client.chat.completions.create(
|
| 40 |
+
model='gpt-4o',
|
| 41 |
+
messages=[{
|
| 42 |
+
"role": "user",
|
| 43 |
+
"content": "Return a JSON object with user profile data"
|
| 44 |
+
}]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
raw_json = response.choices[0].message.content
|
| 48 |
+
|
| 49 |
+
# Fix malformed JSON with jshape
|
| 50 |
+
fixed_json = jshape.repair(raw_json)
|
| 51 |
+
data = jshape.loads(fixed_json)
|
| 52 |
+
|
| 53 |
+
print(data) # ✅ Works even if AI returned malformed JSON
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
## Real-World Example: Image Generation with JSON Config
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
import jshape
|
| 60 |
+
from nexaapi import NexaAPI
|
| 61 |
+
import json
|
| 62 |
+
|
| 63 |
+
client = NexaAPI(api_key='YOUR_API_KEY')
|
| 64 |
+
|
| 65 |
+
def generate_image_from_ai_config(user_request: str) -> str:
|
| 66 |
+
"""
|
| 67 |
+
1. Ask AI to generate image config as JSON
|
| 68 |
+
2. Fix any malformed JSON with jshape
|
| 69 |
+
3. Generate image with NexaAPI
|
| 70 |
+
"""
|
| 71 |
+
# Step 1: Get image config from AI
|
| 72 |
+
config_response = client.chat.completions.create(
|
| 73 |
+
model='gpt-4o',
|
| 74 |
+
messages=[{
|
| 75 |
+
"role": "user",
|
| 76 |
+
"content": f"Generate a JSON config for this image: {user_request}. Include: prompt, width, height, style"
|
| 77 |
+
}]
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
raw_config = config_response.choices[0].message.content
|
| 81 |
+
|
| 82 |
+
# Step 2: Fix malformed JSON (jshape handles edge cases)
|
| 83 |
+
config = jshape.loads(raw_config)
|
| 84 |
+
|
| 85 |
+
# Step 3: Generate image with NexaAPI
|
| 86 |
+
image = client.image.generate(
|
| 87 |
+
model='stable-diffusion-xl',
|
| 88 |
+
prompt=config['prompt'],
|
| 89 |
+
width=config.get('width', 1024),
|
| 90 |
+
height=config.get('height', 1024)
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
return image.image_url
|
| 94 |
+
|
| 95 |
+
# Usage
|
| 96 |
+
url = generate_image_from_ai_config("a futuristic city at sunset")
|
| 97 |
+
print(f"Generated image: {url}")
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## JavaScript Version
|
| 101 |
+
|
| 102 |
+
```javascript
|
| 103 |
+
import NexaAPI from 'nexaapi'; // npm install nexaapi
|
| 104 |
+
import { repair, parse } from 'jshape'; // npm install jshape
|
| 105 |
+
|
| 106 |
+
const client = new NexaAPI({ apiKey: 'YOUR_API_KEY' });
|
| 107 |
+
|
| 108 |
+
async function generateWithAIConfig(userRequest) {
|
| 109 |
+
// Get config from AI
|
| 110 |
+
const configResponse = await client.chat.completions.create({
|
| 111 |
+
model: 'gpt-4o',
|
| 112 |
+
messages: [{ role: 'user', content: `Generate JSON config for: ${userRequest}` }]
|
| 113 |
+
});
|
| 114 |
+
|
| 115 |
+
const rawConfig = configResponse.choices[0].message.content;
|
| 116 |
+
|
| 117 |
+
// Fix malformed JSON
|
| 118 |
+
const config = parse(repair(rawConfig));
|
| 119 |
+
|
| 120 |
+
// Generate image
|
| 121 |
+
const image = await client.image.generate({
|
| 122 |
+
model: 'stable-diffusion-xl',
|
| 123 |
+
prompt: config.prompt,
|
| 124 |
+
width: config.width || 1024,
|
| 125 |
+
height: config.height || 1024
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
return image.imageUrl;
|
| 129 |
+
}
|
| 130 |
+
```
|
| 131 |
+
|
| 132 |
+
## Links
|
| 133 |
+
|
| 134 |
+
- 🌐 [nexa-api.com](https://nexa-api.com) — Get your free NexaAPI key
|
| 135 |
+
- 🚀 [rapidapi.com/user/nexaquency](https://rapidapi.com/user/nexaquency) — Try on RapidAPI
|
| 136 |
+
- 🐍 [pypi.org/project/nexaapi](https://pypi.org/project/nexaapi/) — NexaAPI Python SDK
|
| 137 |
+
- 📦 [npmjs.com/package/nexaapi](https://www.npmjs.com/package/nexaapi) — NexaAPI Node.js SDK
|
| 138 |
+
- 🔧 [pypi.org/project/jshape](https://pypi.org/project/jshape/) — jshape on PyPI
|
| 139 |
+
|
| 140 |
+
## Topics
|
| 141 |
+
|
| 142 |
+
`jshape` `json-repair` `python` `ai-api` `nexaapi` `malformed-json` `developer-tools` `ai-development`
|