{"id":9623,"library":"cornice","title":"Cornice Web Services for Pyramid","description":"Cornice is a Python library that simplifies the definition of RESTful web services within the Pyramid web framework. It allows developers to declare services, resources, and their methods using decorators and schema validation, often integrating with tools like Colander and Marshmallow. The current version is 6.1.0, and it maintains an active release cadence, frequently updating for Python and Pyramid compatibility and addressing dependencies.","status":"active","version":"6.1.0","language":"en","source_language":"en","source_url":"https://github.com/Cornices/cornice","tags":["pyramid","web-services","api","rest","json","schema-validation"],"install":[{"cmd":"pip install cornice","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core web framework integration.","package":"pyramid","optional":false},{"reason":"Optional, for declarative schema validation of request data.","package":"colander","optional":true},{"reason":"Optional, for declarative schema validation of request data. Requires Marshmallow >= 3.0 since Cornice 6.0.0.","package":"marshmallow","optional":true}],"imports":[{"note":"Service is directly exposed at the top level of the cornice package.","wrong":"from cornice.service import Service","symbol":"Service","correct":"from cornice import Service"},{"note":"Used to define resources with multiple services and CRUD operations.","symbol":"resource","correct":"from cornice.resource import resource"},{"note":"Access to built-in validators, often used with Colander schemas.","symbol":"validators","correct":"from cornice import validators"}],"quickstart":{"code":"from wsgiref.simple_server import make_server\nfrom pyramid.config import Configurator\n\nfrom cornice import Service\n\n# 1. Define a Cornice service\nhello_service = Service(name='hello', path='/hello', description=\"Hello World Service\")\n\n@hello_service.get()\ndef get_hello(request):\n    \"\"\"Returns a 'Hello World!' message.\"\"\"\n    # Access request data via request.GET, request.json, request.validated, etc.\n    return {'message': 'Hello World! from Cornice!'}\n\n# 2. Integrate into a Pyramid application\nif __name__ == '__main__':\n    with Configurator() as config:\n        # Include the cornice package to register its components\n        config.include('cornice')\n\n        # Add the defined Cornice service to the Pyramid configuration\n        config.add_cornice_service(hello_service)\n\n        # Create a WSGI application\n        app = config.make_wsgi_app()\n    \n    # 3. Run the Pyramid application\n    server = make_server('0.0.0.0', 6543, app)\n    print('Serving on http://0.0.0.0:6543/hello')\n    print('Press Ctrl+C to quit')\n    try:\n        server.serve_forever()\n    except KeyboardInterrupt:\n        pass\n    finally:\n        server.server_close()","lang":"python","description":"This quickstart demonstrates how to define a simple 'Hello World' service using Cornice within a standalone Pyramid application. Run this script, then access `http://0.0.0.0:6543/hello` in your browser or with `curl`."},"warnings":[{"fix":"Upgrade Marshmallow to version 3 or higher using `pip install 'marshmallow>=3.0.0'`.","message":"Cornice 6.0.0 dropped support for Marshmallow versions older than 3. Attempting to use an older Marshmallow version will lead to compatibility issues or errors.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Migrate your application codebase to Python 3.x to use Cornice 5.0.0 or newer.","message":"Cornice 5.0.0 dropped support for Python 2.x. Applications running on Python 2 will not be compatible with Cornice versions 5.0.0 and above.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"If you had `config.add_renderer('cornice', 'some.renderer.path')` or similar, update it to use `cornicejson` if you relied on the implicit name, or ensure your renderer configuration explicitly maps to the desired name.","message":"In Cornice 5.0.2, the internal default JSON renderer name changed from `cornice` to `cornicejson`. While this change was not officially documented as a breaking change, applications explicitly relying on or configuring a renderer named 'cornice' might experience issues.","severity":"gotcha","affected_versions":">=5.0.2"},{"fix":"Upgrade to Cornice 5.0.3 or higher to ensure Colander validators receive all expected request data for accurate validation.","message":"Prior to Cornice 5.0.3, Colander validators (e.g., `body_validator`) might incorrectly miss `body`, `headers`, `path`, or `querystring` data from the request, leading to incomplete validation.","severity":"gotcha","affected_versions":"<5.0.3"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Change the import to `from cornice import Service`.","cause":"Incorrect import path; 'Service' is directly available in the top-level 'cornice' package, not a submodule.","error":"ImportError: cannot import name 'Service' from 'cornice'"},{"fix":"Use the decorator syntax for defining service methods, e.g., `@service.get()` or `@service.post()`.","cause":"Attempting to use older, deprecated `add_` methods (e.g., `add_get`, `add_post`) instead of the decorator syntax for defining service methods.","error":"AttributeError: 'Service' object has no attribute 'add_get'"},{"fix":"Access request data using dot notation: `request.json` or `request.validated`.","cause":"Attempting to access request attributes like `request['json']` or `request['validated']` using dictionary-style bracket notation.","error":"TypeError: 'Request' object is not subscriptable"},{"fix":"Review your Colander schema definition and ensure the incoming request data (e.g., JSON body, query parameters) matches the expected structure and types. Check logs for specific field errors.","cause":"Validation failed because the request body or parameters do not conform to the defined Colander schema. This often indicates missing required fields or incorrect data types.","error":"colander.Invalid: ... missing or invalid fields ..."}]}